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