Is there a better way to convert from a javascript...
# javascript
t
Is there a better way to convert from a javascript
UInt8Array
back to a Kotlin
ByteArray
than trivially
ByteArray(uintArray.length) { uintArray[it] }
?
d
Since Kotlin's
ByteArray
is represented as
Int8Array
you can do the following to avoid the slow copying:
Copy code
val uint8 = Uint8Array(10)
    val int8 = Int8Array(uint8.buffer)
    val byteArray = int8.unsafeCast<ByteArray>()
Note that if you do this they will continue to share contents
👍 1
If you don't want them to share, you can use the faster native copy method:
Copy code
val uint8 = Uint8Array(10)
val int8 = Int8Array(uint8.buffer.slice(0))
val byteArray = int8.unsafeCast<ByteArray>()
👍 1