我有一个从 UTF8 字符串转换的十六进制字符串 (sA)。当我将十六进制字符串 sA 转换为 UTF8 字符串时,我无法使用构建模式(运行文件 .jar)在表单 UI 中显示它,但是当我使用运行模式或调试模式运行时,UTF8 字符串可以在表单 UI 中显示。我使用 netbeans IDE 7.3.1。我的代码如下:
public String hexToString(String txtInHex) {
byte[] txtInByte = new byte[txtInHex.length() / 2];
int j = 0;
for (int i = 0; i < txtInHex.length(); i += 2) {
txtInByte[j++] = Byte.parseByte(txtInHex.substring(i, i + 2), 16);
}
return new String(txtInByte);
}
private String asHex(byte[] buf) {
char[] chars = new char[2 * buf.length];
for (int i = 0; i < buf.length; ++i) {
chars[2 * i] = HEX_CHARS[(buf[i] & 0xF0) >>> 4];
chars[2 * i + 1] = HEX_CHARS[buf[i] & 0x0F];
}
return new String(chars);
}