简体   繁体   English

如何在Java(Android)中将字节数组转换为字符串

[英]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? 有什么有效的方法可以将字节数组转换为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). 即:您确实应该使用new String(bytes, "UTF-8") (显然,用byte []用来表示文本的任何编码替换了UTF-8)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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