Java byte []到/从String转换 [英] Java byte[] to/from String conversion
问题描述
为什么这次junit测试失败?
Why does this junit test fail?
import org.junit.Assert;
import org.junit.Test;
import java.io.UnsupportedEncodingException;
public class TestBytes {
@Test
public void testBytes() throws UnsupportedEncodingException {
byte[] bytes = new byte[]{0, -121, -80, 116, -62};
String string = new String(bytes, "UTF-8");
byte[] bytes2 = string.getBytes("UTF-8");
System.out.print("bytes2: [");
for (byte b : bytes2) System.out.print(b + ", ");
System.out.print("]\n");
Assert.assertArrayEquals(bytes, bytes2);
}
}
我会假设传入的字节数组等于结果但不知何故,可能由于UTF-8字符占用两个字节这一事实,结果数组在内容和长度上都与传入数组不同。
I would assume that the incoming byte array equaled the outcome, but somehow, probably due to the fact that UTF-8 characters take two bytes, the outcome array differs from the incoming array in both content and length.
请赐教。
推荐答案
原因是 0,-121,-80,116,-62
不是有效的UTF-8字节序列。 new String(bytes,UTF-8)在这种情况下不会抛出任何异常,但结果很难预测。阅读 http://en.wikipedia.org/wiki/UTF-8 无效的字节序列部分。
The reason is 0, -121, -80, 116, -62
is not a valid UTF-8 byte sequence. new String(bytes, "UTF-8") does not throw any exception in such situations but the result is difficult to predict. Read http://en.wikipedia.org/wiki/UTF-8 Invalid byte sequences section.
这篇关于Java byte []到/从String转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!