2

I'm trying to convert a byte array to a string. The problem is that UTF-8 won't work. I can't show "ü", "ä" or something like this. Can anyone help me?

public static String byteToString(byte[] bytes) {
    String str = new String(bytes, Charsets.UTF_8);

    return str;
}

Would be nice if it works down until API 8 Android

3
  • have you try new String(bytes, "UTF-8");? Commented Jul 14, 2014 at 5:22
  • That should work. Are you sure the data is UTF-8? How do you display it back? Commented Jul 14, 2014 at 5:23
  • Requires at least API 9. Yes I'm sure that the data is UTF-8 and I display it in a TextView with .setText() Commented Jul 14, 2014 at 14:14

1 Answer 1

2

Try this instead:

String str = new String(bytes, Charset.forName("UTF8"));
Sign up to request clarification or add additional context in comments.

2 Comments

@Phil Just to help you better, can you please update your question by adding two things?: 1 - the byte array input, and 2 - the String you're expecting from such byte array
This does not work, It works in C# but not in Java.

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.