【发布时间】:2018-01-06 23:28:56
【问题描述】:
我有一个 GUI,我尝试将 EBCDIC 字符串转换为十六进制,但它不起作用:/
ebcdichex.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
output6.setText("");
String hexadecimal3 = input4.getText().replace("\n", "");
byte[] chars2;
try {
chars2 = hexadecimal3.getBytes("IBM-273");
StringBuilder hexa2 = new StringBuilder();
for(int i = 0;i<chars2.length;i++){
byte[] b = {(byte) Integer.parseInt(chars2.toString(),16)};
hexa2.append(b);
}
output6.append(hexa2.toString());
}
catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
}
});
示例输入:
f1f2f3f4
翻译:
1234
它只是抛出这个错误:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "[B@147c5fc"
这段代码有什么问题?
【问题讨论】:
-
chars2.toString()?byte[]数组不会覆盖toString();它只是从Object类继承而来。 -
考虑问你真正想问的问题。您不是在尝试将 EBCDIC 转换为十六进制,而是将 EBCDIC 转换为 ASCII。
-
哦,好吧,但我真的想把它转换成十六进制:O
-
考虑添加“预期输出”部分。
标签: java string hex converter ebcdic