java - ASCII 到 EBCDIC 字符编码

标签 java character-encoding hex ascii ebcdic

将 ASCII 字符串转换为 EBCDIC 时:

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

我得到下面的输出字符串:

ä??????

但是,我想要的是:

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

如何实现?任何帮助将不胜感激。

谢谢

最佳答案

你可以这样转换字符串

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]);
}

但是你的例子似乎是错误的。

以下是正确的,输入字符串中的一个字符被转换为相关的EBCDIC码

0 F0
8 F8
1 F1
0 F0

这里您的示例是错误的,因为您的示例将 C220 视为输入字符串中的两个字符,而不是 EBCDIC 代码中的两个字符

C C3
2 F2
2 F2
0 F0

对于另一个方向的转换,你可以这样做

// 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");

关于java - ASCII 到 EBCDIC 字符编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29691239/

相关文章:

java - 在 get 请求中将 JSON 数据作为请求体传递

linux - 二进制数如何表示为字符

python - 如何使用 python 将带符号的整数打印为二进制补码的十六进制数?

java - 无法使用 HIbernate 在 JBoss 上加载请求的类 : com. mysql.jdbc.Driver

java - 控制台输入在不应该等待的时候等待

java - JSF 和库的不明确性

java - 为什么我的加密字符串看起来只包含问号?

java - 如何在 Jackcess 中导入特定编码的文件?

java - Java读取文件的前10个字节

Python:如何比较两个二进制文件?