https://kotlinlang.org logo
Title
s

Sylvain Patenaude

11/28/2019, 9:04 PM
Is there a "pure" Kotlin way to convert a UByteArray into a UTF-8 string? Thanks! Edit: If there's no built-in support for this, can I just convert my array (UByteArray -> ByteArray) and then call
decodeToString
on it?
s

Shawn

11/28/2019, 10:39 PM
I don’t think you’ll want to convert from UByte to Byte unless you somehow don’t care about truncation or sign shenanigans
e

E.Kisaragi

11/28/2019, 10:42 PM
call String(ByteArray, Charset)
but there’s undefined behavior if the array contains invalid sequence
i

ilya.gorbunov

11/29/2019, 12:36 AM
You can reinterpret an UByteArray as a ByteArray (by calling
asByteArray()
on it) and then call
decodeToString()
s

Sylvain Patenaude

11/29/2019, 2:23 PM
Hmm, I hadn't seen
asByteArray()
yet. Might be more adequate then the
toByteArray()
I used.