Hally
Hally

Reputation: 81

How can I convert Bytes Array to String on Kotlin other than String()?

[51, -42, 119, -85, -64, 126, 22, 127, -72, 72, 48, -66, -18, 45, 99, -119]

This is the BytesArray that I want to print in String. When I searched on the internet, I found that

String(Bytes, Charsets.UTF_8) 

would convert it to String. However, I get �؉���Q�t, and doesn't seem to be converted in right way. Why is it?

Upvotes: 0

Views: 2089

Answers (1)

Matthew Layton
Matthew Layton

Reputation: 42250

I want it to be String in Alphabet characters and numbers

Firstly, you are specifying an array of signed bytes (indicated by negative numbers):

51, -42, 119, -85, -64, 126, 22, 127, -72, 72, 48, -66, -18, 45, 99, -119

Let's take a look at what this would hypothetically look like if it were unsigned (I used this tool for the conversion):

51, 214, 119, 171, 192, 126, 22, 127, 184, 72, 48, 190, 238, 45, 99, 137

Assuming by "Alphabet characters and numbers", you mean the English alphabet, then asciitable will help you identify each character's decimal value, but as a rough guide:

  • "0"-"9" = 48-57
  • "A"-"Z" = 65-90
  • "a"-"z" = 97-122

Consider the following code sample:

/**
 * You can edit, run, and share this code. 
 * play.kotlinlang.org 
 */

fun main() {
    val bytes = byteArrayOf(51, -42, 119, -85, -64, 126, 22, 127, -72, 72, 48, -66, -18, 45, 99, -119)
    val string = bytes.toString(Charsets.US_ASCII)
    
    println(string)
}

As you can see, some of the values in the unsigned array fall outside of the range for English alphabetic characters and numbers, which is why you end up with a string, something like this "3�w��~�H0��-c�" depending on the charset you choose.

For reference:

Charset Result
Charsets.US_ASCII 3�w��~�H0��-c�
Charsets.UTF_8 3�w��~�H0��-c�
Charsets.UTF_16 ㏖瞫쁾ᙿ롈ゾ掉
Charsets.UTF_32 ����
Charsets.ISO_8859_1 3Öw«À~¸H0¾î-c

So, it really depends on exactly which encoding the array is using, and exactly what it is you're expecting the resulting string to be.

You can play with the code above, here.

Upvotes: 1

Related Questions