Shan
09/02/2022, 11:57 PMUint8Array
into a utf-8 string. Anyone know the best practice way to do this, since a Uint8Array
isn't back by a UByteArray
internally (from what I can tell) ?turansky
09/03/2022, 12:06 AMShan
09/03/2022, 12:07 AMTextDecoder
from JS yeah, just not sure how to instantiate it from Kotlin with the js new
syntax.turansky
09/03/2022, 12:08 AMval encoder = TextEncoder()
Shan
09/03/2022, 12:08 AMTextEncoder
has to be defined somewhere as External
no? And it's constructor?Shan
09/03/2022, 12:12 AMfun TextDecoder(): dynamic = js("new TextDecoder()")
?turansky
09/03/2022, 12:15 AMexternal class TextEncoder() {
// required methods
}
turansky
09/03/2022, 12:22 AMShan
09/03/2022, 12:24 AMTextDecoder
apparently, my Uint8Array
is too large and TextDecoder('utf-8').decode(bytes)
fails with RangeError: Maximum call stack size exceeded in json object.
Seeking other alternatives now.. I might have to chunk this or something.Shan
09/03/2022, 12:30 AMprintln("bytes: $bytes")
where bytes
is an Uint8Array
and it prints out a nice utf-8 encoded string in my console, but when I call bytes.toString()
it prints out [...]
? Doesn't embedding something in a string just call its toString()
internally? Not sure how I could be getting different behavior when I call it manually.ephemient
09/03/2022, 4:44 AM"bytes: $bytes"
== "bytes: " + bytes
which Kotlin translates to exactly that in JS. this leads JS to coerce bytes
to a String
, which results in the decoding behavior you seeephemient
09/03/2022, 4:48 AM.toString()
goes through Kotlin's stringification implementation which treats Array
specially, and Uint8Array
is an Array
ephemient
09/03/2022, 6:50 AM(arr.asDynamic().toString)().unsafeCast<String>()
will call JS's .toString()
, not Kotlin'sturansky
09/03/2022, 9:44 AM"$bytes"
?ephemient
09/04/2022, 3:47 AM'' + bytes
, so that would work too, I'm not sure how documented/futureproof that is thoughShan
09/04/2022, 9:48 PMturansky
12/01/2022, 12:32 PMTextDecoder
generated and will be available in next release of wrappers