您可以通过这种方式转换字符串
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
在这里,您的示例是错误的,因为您的示例将C2
and20
视为输入字符串中的两个字符,而不是 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");