このjunitテストが失敗するのはなぜですか?
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 文字が 2 バイトかかるという事実が原因で、内容と長さの両方で結果配列が着信配列と異なります。
教えてください。