通过String.getBytes(charset)转换为EBCDIC至少提供一个错误结果。字符“a”变为0x3f,但应为0x81。
public static void convert() throws UnsupportedEncodingException {
String data="abcABC";
String ebcdic = "IBM-1047";
String ascii = "ISO-8859-1";
System.out.printf("Charset %s is supported: %s\n", ebcdic, Charset.isSupported(ebcdic));
String result= new String(data.getBytes(ebcdic));
System.out.printf("EBCDIC: %s\n",asHex(result.getBytes()));
System.out.printf("Charset %s is supported: %s\n", ascii, Charset.isSupported(ascii));
result= new String(data.getBytes(ascii));
System.out.printf("ASCII: %s\n",asHex(result.getBytes()));
}
public static String asHex(byte[] buf) {
char[] HEX_CHARS = "0123456789abcdef".toCharArray();
char[] chars = new char[2 * buf.length];
for (int i = 0; i < buf.length; ++i)
{
chars[2 * i] = HEX_CHARS[(buf[i] & 0xF0) >>> 4];
chars[2 * i + 1] = HEX_CHARS[buf[i] & 0x0F];
}
return new String(chars);
}
结果是:
我能做些什么吗?
答案 0 :(得分:3)
致电时
data.getBytes(ebcdic)
您正在将数据中的文本编码为EBCDIC字节。然后从这些字节创建一个字符串,好像它们代表系统的默认字符编码中的某个字符串:这会导致破坏,因为字节不必使用除EBCDIC之外的任何其他编码来编码有效文本。
要解决此问题,请将字节保留为字节:
byte[] result= data.getBytes(ebcdic);
System.out.printf("EBCDIC: %s\n",asHex(result));