0
[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?

5
  • 3
    What do you expect the resulting string to be? Commented Jul 13, 2021 at 15:03
  • I want it to be String in English Alphabet and numbers Commented Jul 13, 2021 at 15:54
  • 1
    Here's a blog post by one of the founders of Stack Overflow: The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!). Commented Jul 13, 2021 at 16:15
  • Where do the bytes come from? How do you know that the bytes can be represented as English text? What encoding is it in? Commented Jul 13, 2021 at 16:18
  • Or maybe you want to encode it to hexadecimal or base 64? The question is unanswerable without more details. Commented Jul 13, 2021 at 16:26

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.