Let me know if there’s a better channel to post th...
# getting-started
s
Let me know if there’s a better channel to post this in: I’ve got a
UInt8
encoded byte array that I need to convert to a UTF-8 string, using Kotlin. various things I’ve tried have converted it into the ASCII value “d”, which is the correct value. But I want the UTF-8 string value “100"
Got it (but assume there’s a better way):
UInt8
byte array to string:
it.decodeToString()
string to char:
string.first()
(I know it’s a single value which is why I can call first() ) char to decimal:
char.code
okay that was royally dumb. Here’s something better
val test = it[0].toInt()
j
How do you know your single character fits in one byte? Your last attempt assumes that, but that's not how UTF-8 works
s
I do happen to know that what I’ve got fits into one byte. But, yes, my solution is brittle for scenarios where what I get doesn’t fit into a single byte.
e
if your byte is not in the range of 0-127, then it is definitely not valid UTF-8 on its own
and `Char.code`/`Char.toInt` doesn't account for surrogates pairs (since
Char
is a UTF-16 code unit for historical reasons)
s
thanks and keep the comments coming! (I’m taking notes)
e
even if you use
String.codePointAt(0)
you'll have to decide whether that's actually what you want or if you want to deal with normalization forms, combining sequences, emoji modifier sequences, flags, etc.