【发布时间】:2014-07-08 13:35:53
【问题描述】:
最近我在从字节转换为字符串然后从字符串转换为字节的过程中遇到了一个奇怪的 Cp500 (EBCDIC) 编码问题。
问题是一个特定字符 LINE FEED - LF - 0x25 在此转换期间被转换为此字符 NEW LINE - NEL - 0x15。
下面的代码验证了这一点:
byte[] b25 = { 0x25 };
byte[] b4E = { 0x4E };
System.out.printf("\n0x25 in hex : <0x%02X>", b25[0]);
System.out.printf("\n0x4E in hex : <0x%02X>", b4E[0]);
String stringB25 = new String(b25, "Cp500");
String stringB4E = new String(b4E, "Cp500");
System.out.printf("\nOther way, 0x25 in hex : <0x%02X>", stringB25.getBytes("Cp500")[0]);
System.out.printf("\nOther way, 0x4E in hex : <0x%02X>", stringB4E.getBytes("Cp500")[0]);
输出:
0x25 in hex : <0x25>
0x4E in hex : <0x4E>
Other way, 0x25 in hex : <0x15>
Other way, 0x4E in hex : <0x4E>
为了理解这种行为,我查看了 IBM500.java 类,发现 0x15 和 0x25 字符映射到“\n”字符。
这背后的原因是什么?
最终,有没有办法在字符串编码和解码之间保持字节输入的一致性机制?
【问题讨论】:
标签: java encoding byte java-6 codepages