简体   繁体   English

ASCII到EBCDIC字符编码

[英]ASCII to EBCDIC Character Incoding

While converting ASCII String to EBCDIC: 将ASCII字符串转换为EBCDIC时:

System.out.println(new String("0810C2200000820000000400000000000000052852304131419391011590620022300270".getBytes("UTF-8"), "CP1047"));

I am getting below as output String: 我得到以下作为输出字符串:

ä??????

But, what I want is: 但是,我想要的是:

F0 F8    F1 F0    C2 20    00 00    82 00    00 00    04 00  00 00    00 00    00 00   F4 F1    F0 F1    F1 F5  F9 F0    F6 F2    F0 F0    F2 F2    F3 F0    F0 F2    F7 F0

How I can achieve it? 我该如何实现? Any help will be appreciated. 任何帮助将不胜感激。

Thanks 谢谢

You can convert the string this way 你可以这样转换字符串

String string = "0810C220";
byte[] bytes = string.getBytes("CP1047");
for (int i = 0; i < bytes.length; i++) {
    System.out.printf("%s %X%n", string.charAt(i), bytes[i]);
}

But your example seems to be wrong. 但是您的示例似乎是错误的。

following are correct, one character from input string is converted to the related EBCDIC code 以下是正确的,将输入字符串中的一个字符转换为相关的EBCDIC代码

0 F0
8 F8
1 F1
0 F0

here your example is wrong, because your example treats C2 and 20 as two characters in the input string but not as two characters in the EBCDIC code 这里的示例是错误的,因为您的示例将C220视为输入字符串中的两个字符,而不是EBCDIC代码中的两个字符

C C3
2 F2
2 F2
0 F0

For the conversion in the other direction you could do it that way 对于另一个方向的转换,您可以这样做

// string with hexadecimal EBCDIC codes
String sb = "F0F8F1F0";
int countOfHexValues = sb.length() / 2;
byte[] bytes = new byte[countOfHexValues];
for(int i = 0; i < countOfHexValues; i++) {
    int hexValueIndex = i * 2;
    // take one hexadecimal string value
    String hexValue = sb.substring(hexValueIndex, hexValueIndex + 2);
    // convert it to a byte
    bytes[i] = (byte) (Integer.parseInt(hexValue, 16) & 0xFF);
}
// constructs a String by decoding bytes as EBCDIC
String string = new String(bytes, "CP1047");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM