我必须将一些包含本地化值的字符串保存到字节数组中。
然后,我必须从字节数组重新构造字符串
我将字符串编码为字节数组,如下所示:
byte addr = 0x08;
for (String s : values) {
char[] dataBytes = s.toCharArray();
int length = s.length();
for (int i = 0; i < 4; i++) {
byte[] buffer = new byte[4];
for (int j = 0; j < 4; j++) {
if(i * 4 + j < length ) {
buffer[j] = (byte) Character.codePointAt(dataBytes, i * 4 + j);
} else {
buffer[j] = (byte) 0;
}
}
nfcHandler.write(buffer, addr);
addr++;
TimeUnit.MILLISECONDS.sleep(10);
}
}如何重建字符串?我试过了
new String(bytes, StandardCharsets.UTF_8);和
bytes.toString(); 但这两种方法都不起作用。
你知道怎么做吗?
请帮帮我...!
发布于 2019-04-18 14:48:46
将字节数组解码为String的方式很好,但在编码方面做了太多的工作。请改用String.getBytes()方法:
byte[] buffer = s.getBytes(StandardCharsets.UTF_8);
// use buffer at needed...https://stackoverflow.com/questions/55728718
复制相似问题