java 如何在十六进制转换时转义无法识别的字符
java how to escape unrecognized character on hexadecimal conversion
来自 WireShark 的数据:
数据是
0000 01 01 00 b6 00 00 01 00 16 00 00 00 12 00 00 00
0010 02 00 00 00 00 00 00 00 00 00 01 00 00 00 53 00
0020 45 00 4c 00 45 00 43 00 54 00 20 00 2a 00 0d 00
0030 0a 00 20 00 20 00 46 00 52 00 4f 00 4d 00 20 00
0040 5b 00 56 00 69 00 73 00 61 00 4f 00 72 00 64 00
0050 65 00 72 00 44 00 42 00 5d 00 2e 00 5b 00 64 00
0060 62 00 6f 00 5d 00 2e 00 5b 00 4c 00 65 00 61 00
0070 64 00 43 00 6f 00 75 00 72 00 73 00 65 00 5d 00
0080 0d 00 0a 00 20 00 20 00 77 00 68 00 65 00 72 00
0090 65 00 20 00 43 00 6f 00 75 00 72 00 73 00 65 00
00a0 3d 00 20 00 27 00 cf 6b 63 5b a6 5e 27 6b 32 6d
00b0 fe 8b 0b 7a 27 00
传输后应该是这样的:
SELECT *
FROM [VisaOrderDB].[dbo].[LeadCourse]
where Course= '每季度欧洲课程'
我的java代码如下:
public static String hexString2String(String src) {
String temp = "";
for (int i = 0; i < src.length() / 2; i++) {
temp = temp
+ (char) Integer.valueOf(src.substring(i * 2, i * 2 + 2),
16).byteValue();
}
return temp;
}
public static void main(String args[]) {
System.out.println(hexString2String("010100b60000010016000000120000000200000000000000000001000000530045004c0045004300540020002a000d000a0020002000460052004f004d0020005b0056006900730061004f007200640065007200440042005d002e005b00640062006f005d002e005b004c0065006100640043006f0075007200730065005d000d000a002000200077006800650072006500200043006f0075007200730065003d0020002700cf6b635ba65e276b326dfe8b0b7a2700"));
}
结果如下:
有人知道怎么解决吗?
您的数据不是 ASCII(也不是 UTF8):它是 UTF16(您可以看到它以 2 个字节对单个字符进行编码)。此外,您正在捕获数据包,因此您需要先去除包头 - 这不是您要查找的文本。
解码 UTF-16 数据(我使用您的代码作为起点):
public static String hexString2String(String src) {
byte[] data = new byte[src.length() / 2];
for (int i = 0; i < src.length() / 2; i++) {
data[i] = Integer.valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue();
}
return new String(data, StandardCharsets.UTF_16LE);
}
使用DatatypeConverter.parseHexBinary:
String s = "010100b6000001001600000012000000" +
"02000000000000000000010000005300" +
"45004c0045004300540020002a000d00" +
"0a0020002000460052004f004d002000" +
"5b0056006900730061004f0072006400" +
"65007200440042005d002e005b006400" +
"62006f005d002e005b004c0065006100" +
"640043006f0075007200730065005d00" +
"0d000a002000200077006800650072006" +
"500200043006f0075007200730065003d" +
"0020002700cf6b635ba65e276b326dfe8b" +
"0b7a2700";
String result = new String(DatatypeConverter.parseHexBinary(s), StandardCharsets.UTF_16LE);
System.out.print(result);
来自 WireShark 的数据:
数据是
0000 01 01 00 b6 00 00 01 00 16 00 00 00 12 00 00 00
0010 02 00 00 00 00 00 00 00 00 00 01 00 00 00 53 00
0020 45 00 4c 00 45 00 43 00 54 00 20 00 2a 00 0d 00
0030 0a 00 20 00 20 00 46 00 52 00 4f 00 4d 00 20 00
0040 5b 00 56 00 69 00 73 00 61 00 4f 00 72 00 64 00
0050 65 00 72 00 44 00 42 00 5d 00 2e 00 5b 00 64 00
0060 62 00 6f 00 5d 00 2e 00 5b 00 4c 00 65 00 61 00
0070 64 00 43 00 6f 00 75 00 72 00 73 00 65 00 5d 00
0080 0d 00 0a 00 20 00 20 00 77 00 68 00 65 00 72 00
0090 65 00 20 00 43 00 6f 00 75 00 72 00 73 00 65 00
00a0 3d 00 20 00 27 00 cf 6b 63 5b a6 5e 27 6b 32 6d
00b0 fe 8b 0b 7a 27 00
传输后应该是这样的:
SELECT *
FROM [VisaOrderDB].[dbo].[LeadCourse]
where Course= '每季度欧洲课程'
我的java代码如下:
public static String hexString2String(String src) {
String temp = "";
for (int i = 0; i < src.length() / 2; i++) {
temp = temp
+ (char) Integer.valueOf(src.substring(i * 2, i * 2 + 2),
16).byteValue();
}
return temp;
}
public static void main(String args[]) {
System.out.println(hexString2String("010100b60000010016000000120000000200000000000000000001000000530045004c0045004300540020002a000d000a0020002000460052004f004d0020005b0056006900730061004f007200640065007200440042005d002e005b00640062006f005d002e005b004c0065006100640043006f0075007200730065005d000d000a002000200077006800650072006500200043006f0075007200730065003d0020002700cf6b635ba65e276b326dfe8b0b7a2700"));
}
结果如下:
有人知道怎么解决吗?
您的数据不是 ASCII(也不是 UTF8):它是 UTF16(您可以看到它以 2 个字节对单个字符进行编码)。此外,您正在捕获数据包,因此您需要先去除包头 - 这不是您要查找的文本。
解码 UTF-16 数据(我使用您的代码作为起点):
public static String hexString2String(String src) {
byte[] data = new byte[src.length() / 2];
for (int i = 0; i < src.length() / 2; i++) {
data[i] = Integer.valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue();
}
return new String(data, StandardCharsets.UTF_16LE);
}
使用DatatypeConverter.parseHexBinary:
String s = "010100b6000001001600000012000000" +
"02000000000000000000010000005300" +
"45004c0045004300540020002a000d00" +
"0a0020002000460052004f004d002000" +
"5b0056006900730061004f0072006400" +
"65007200440042005d002e005b006400" +
"62006f005d002e005b004c0065006100" +
"640043006f0075007200730065005d00" +
"0d000a002000200077006800650072006" +
"500200043006f0075007200730065003d" +
"0020002700cf6b635ba65e276b326dfe8b" +
"0b7a2700";
String result = new String(DatatypeConverter.parseHexBinary(s), StandardCharsets.UTF_16LE);
System.out.print(result);