我知道在python binascii.unhexlify(initValue)
 返回由十六进制字符串表示的二进制数据。

我正在尝试将binascii.unhexlify(initValue)转换为java。

我在Java中尝试了以下代码行,但是与python中的代码相比,我得到了不同的结果:

 DatatypeConverter.parseHexBinary(value);


我运行以下示例:

我的输入-十六进制字符串:

 value = '270000f31d32d1051400000000000000000000000006000000000000000000000000000000000000'


在python中运行时:

 result = binascii.unhexlify(value)


我正进入(状态:

    result = "'\x00\x00\xf3\x1d2\xd1\x05\x14\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"


在Java中运行时:

 byte[] bytes = DatatypeConverter.parseHexBinary(value);


我正进入(状态:

   bytes = [39, 0, 0, -13, 29, 50, -47, 5, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]


1.为什么我得到不同的结果?


为什么我在python中得到带有'\'标记的输出?

最佳答案

结果的第一个十六进制“'”在带符号的char中正好是39。在python中,您可以使用内置函数ord(“'”)获得39。
您可能可以在此python代码中获得所需的内容

value = '270000f31d32d1051400000000000000000000000006000000000000000000000000000000000000'
result = binascii.unhexlify(value)
bytes = [ord(x) for x in result]


您将得到以下未签名的字符:

[39, 0, 0, 243, 29, 50, 209, 5, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

10-04 11:40
查看更多