我希望将字节数组转换为String,但是这样做的时候,我的String在从数组中获取每个数字之前都有00。
我应该得到以下结果:49443a3c3532333437342e313533373936313835323237382e303e
但是我有以下内容:
请帮助我,我怎样才能消除空值?
我尝试了以下方法进行转换:
xxxxId是byteArray
String xxxIdString =新的String(Hex.encodeHex(xxxxId));
谢谢!
答案 0 :(得分:3)
尝试这样的事情:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
dataGridView1[e.ColumnIndex, e.RowIndex].Value = 1;
}
这也是可能的,字符串将在收到第一个\ 0后结束,如果是这样,请首先遍历数组,并用'\ n'替换'\ 0'并执行以下操作:>
String s = new String(bytes);
s = s.replace("\0", "")
编辑: 将此用于BYTE-ARRAY:
String s = new String(bytes);
s = s.replace("\n", "")
将此用于CHAR:
String s = new String(bytes, StandardCharsets.UTF_8);
答案 1 :(得分:0)
为了正确地将Byte数组转换为String格式,我们必须显式创建一个String对象并为其分配Byte数组。
String example = "This is an example";
byte[] bytes = example.getBytes();
String s = new String(bytes);
答案 2 :(得分:0)
尝试以下代码:
byte[] bytes = {...}
String str = new String(bytes, "UTF-8"); // for UTF-8 encoding