# Approaches to Convert to/from Longs in Kotlin JS

**URL:** <https://discuss.kotlinlang.org/t/approaches-to-convert-to-from-longs-in-kotlin-js/8075>\
**Category:** Support\
**Created:** [June 7, 2018, 7:09pm UTC](https://discuss.kotlinlang.org/t/approaches-to-convert-to-from-longs-in-kotlin-js/8075 "2018-06-07T19:09:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cretz](https://avatars.discourse-cdn.com/v4/letter/c/d2c977/32.png) [@cretz](https://discuss.kotlinlang.org/u/cretz)\
**Post date:** [June 7, 2018, 7:09pm UTC](https://discuss.kotlinlang.org/t/approaches-to-convert-to-from-longs-in-kotlin-js/8075/1 "2018-06-07T19:09:57Z")

</div>

I am using a third party JS library that creates longs the same way Kotlin has them, with two fields that are high and low bits. But Kotlin doesn’t expose these two values outside of their JS impl. I essentially need both the constructor and access to the high and low bits from [https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/js/src/js/long.js](https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/js/src/js/long.js). Is this a reasonable request and worthy of an issue? In the meantime, what is the best way to do this, even in a hacky way?

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [June 7, 2018, 9:17pm UTC](https://discuss.kotlinlang.org/t/approaches-to-convert-to-from-longs-in-kotlin-js/8075/2 "2018-06-07T21:17:37Z")

</div>

You should be able to expose those functions like this

```kotlin
fun Long.Companion.fromBits(lowBits: Int, highBits: Int): Long 
    = js("Kotlin").Long.fromBits(lowBits, highBits) as Long

```

---

<div class="post-metadata">

**Author:** ![cretz](https://avatars.discourse-cdn.com/v4/letter/c/d2c977/32.png) [@cretz](https://discuss.kotlinlang.org/u/cretz)\
**Post date:** [June 7, 2018, 9:32pm UTC](https://discuss.kotlinlang.org/t/approaches-to-convert-to-from-longs-in-kotlin-js/8075/3 "2018-06-07T21:32:06Z")

</div>

Yeah, that’s what I’ll do and `getHighBits`/`getLowBits` in the other direction. I fear it may change out from under me though but oh well.

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [June 7, 2018, 9:41pm UTC](https://discuss.kotlinlang.org/t/approaches-to-convert-to-from-longs-in-kotlin-js/8075/4 "2018-06-07T21:41:18Z")

</div>

Well the js source of this was never edited before (except for added copyright) so I think you are safe.
