gpt4 book ai didi

java 如何在十六进制转换中转义无法识别的字符

转载 作者:行者123 更新时间:2023-11-30 06:57:49 26 4
gpt4 key购买 nike

来自 WireShark 的数据:

data from 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"));
}

结果是这样的:

java result picture

有人知道如何解决吗?

最佳答案

您的数据不是 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);
}

关于java 如何在十六进制转换中转义无法识别的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41480071/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com