What's the best and fastest way to convert a byte array to a String.
The (toString doesn't work, it gives strange characters).
I prefere a way that uses a loop that loops in the bytes of the array
and convert them to characters then append them to a string.
Thanks.
> What's the best and fastest way to convert a byte array to a String.
> The (toString doesn't work, it gives strange characters).
[quoted text clipped - 3 lines]
>
> Thanks.
How about one of the variants of this, which allows you to specify a
character set encoding?
http://java.sun.com/javase/6/docs/api/java/lang/String.html#String(byte[],%20jav
a.lang.String)
Karl Uppiano - 18 Mar 2007 00:06 GMT
>> What's the best and fastest way to convert a byte array to a String.
>> The (toString doesn't work, it gives strange characters).
[quoted text clipped - 8 lines]
>
> http://java.sun.com/javase/6/docs/api/java/lang/String.html#String(byte[],%20jav
a.lang.String)
That URL was supposed to take you to this String constructor:
public String(byte[] bytes, String charsetName)
throws UnsupportedEncodingException
The charsetName tells the constructor how to interpret the bytes in the byte
array -- they could be UTF-8, ASCII, EBCDIC, or Swahili, which explains the
strange characters you get from toString, which just uses the default
encoding for your platform.