简体   繁体   中英

How to convert byte array to string in Java (Android)

I am converting byte array to String by:

public static String byteArrayToString(byte[] bytes)
{
    return new String(bytes);
}

But this approach is taking much time. Is there any efficient way to convert byte array to String?

There is no better way that I know of. And you should always use the constructor that takes the encoding with it, or its pretty much guaranteed that you'll end up with screwed up characters if you deal with any language other than english. ie: you really should be using new String(bytes, "UTF-8") (obviously replacing UTF-8 by whatever encoding your byte[] is using to represent the text).

字符串头=新字符串(byteArray,“ ISO-8859-1”);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM