[英]Converting byte array containing ASCII characters to a String
I have a byte array that consists of ASCII characters that I wish to convert to a String. 我有一个字节数组,由我希望转换为String的ASCII字符组成。 For example:
例如:
byte[] myByteArray = new byte[8];
for (int i=0; i<8; i++) {
byte[i] = (byte) ('0' + i);
}
myByteArray should contain a string "12345678" after the loop. 循环后,myByteArray应包含字符串“12345678”。 How do I get this string into a String variable?
如何将此字符串转换为String变量?
Thanks! 谢谢!
Use 使用
new String(myByteArray, "UTF-8");
String class provides a constructor for this. String类为此提供了构造函数 。
Side note:The second argument here is the CharSet (byte encoding) which should be handled carefully. 旁注:这里的第二个参数是CharSet (字节编码),应该仔细处理。 More here.
更多这里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.