将包含ASCII字符的字节数组转换为字符串 [英] Converting byte array containing ASCII characters to a String

查看:917
本文介绍了将包含ASCII字符的字节数组转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字节数组,其中包含我希望转换为字符串的ASCII字符.例如:

I have a byte array that consists of ASCII characters that I wish to convert to a String. For example:

byte[] myByteArray = new byte[8];
for (int i=0; i<8; i++) {
    byte[i] = (byte) ('0' + i);
}

myByteArray在循环后应包含字符串"12345678".如何将此字符串转换为String变量?

myByteArray should contain a string "12345678" after the loop. How do I get this string into a String variable?

谢谢!

推荐答案

使用

new String(myByteArray, "UTF-8");

String类提供了

String class provides a constructor for this.

旁注:此处的第二个参数是 CharSet (字节编码),应小心处理. 此处更多.

Side note:The second argument here is the CharSet(byte encoding) which should be handled carefully. More here.

这篇关于将包含ASCII字符的字节数组转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆