简体   繁体   English

如何解码此小端(int32)消息?

[英]How to decode this little endian(int32) message?

I have a task to do. 我有任务要做 I get lot of data and my job is to decode it. 我得到了很多数据,我的工作是解码它。 Every piece I get is written in readable string of hex numbers. 我得到的每一部分都以可读的十六进制数字字符串编写。 For example: "8a fd ff ff" 例如:“ 8a fd ff ff”

I tried with struct.unpack('<l', "8a fd ff ff") and I also tried a lot of way to decode and it would be too long to list. 我尝试使用struct.unpack('<l', "8a fd ff ff") ,并且尝试了很多解码方式,因此列出它太长了。 Only thing I know that this should give me -630 as a result. 我只知道这应该使我得到-630。 I don't know how the data was transformed to this form. 我不知道如何将数据转换为这种形式。

data = "8a fd ff ff"
aa = np.array(list(data))
print(aa)
struct.unpack('<l', aa)

struct.error: unpack requires a buffer of 4 bytes struct.error:解压缩需要4个字节的缓冲区

so the result should be -630. 因此结果应为-630。 I tested it with an online hex decoder. 我使用在线十六进制解码器对其进行了测试。

A find the first answer good, but a more intuitive, and easier to remember is following. 找到第一个答案很好,但是更直观,更容易记住。 Given that all of your data is given as a space separated string of hex numbers you can use : 鉴于您的所有数据均以空格分隔的十六进制数字字符串形式提供,您可以使用:

data = "8a fd ff ff"
bytes_data = bytes.fromhex( data.strip() ) 
struct.unpack("<l", bytes_data)

In this example we first load the data, remove all the whitespaces, convert to binary hex representation and read it out. 在此示例中,我们首先加载数据,删除所有空格,转换为二进制十六进制表示形式并将其读出。 Output is as you expect it to be. 输出与您期望的一样。

Step-wise conversion from hex string to byte string 从十六进制字符串到字节字符串的逐步转换

unpack() expects a byte string as the second argument (which must match the format as specified by the first argument). unpack()期望将字节字符串作为第二个参数(必须与第一个参数指定的格式匹配)。 However, what we have now in data is '8a fd ff ff' , which is a hex string. 但是,我们现在data'8a fd ff ff' ,它是一个十六进制字符串。 So we need to convert the hex string into byte string by taking the steps below: 因此,我们需要按照以下步骤将十六进制字符串转换为字节字符串:

  1. Split the data: 分割数据:
data_split = data.split()
# ['8a', 'fd', 'ff', 'ff']
  1. Now, the split data are still hex strings, so we need to read them as hex using int() , passing in 16 as the second argument because hexadecimal is base 16: 现在,拆分数据仍然是十六进制字符串,因此我们需要使用int()将它们读取为十六进制,因为十六进制是基数16,所以将16作为第二个参数传递
data_converted = [int(hex_string, 16) for hex_string in data_split]
# [138, 253, 255, 255]
  1. We can now convert the data to bytes: 现在我们可以将数据转换为字节:
byte_array = [chr(int_) for int_ in data_converted]
# ['\x8a', '\xfd', '\xff', '\xff']
  1. We then join the array into a single string: 然后,我们将数组连接到单个字符串中:
byte_string = ''.join(byte_array)
# '\x8a\xfd\xff\xff'
  1. Finally, we can use unpack() , with the l format character for long (ie, 4 bytes): 最后,我们可以将unpack()l格式字符一起使用long (即4个字节):
unpack('<l', byte_string)
# (-630,)

struct.unpack doesn't expect a string but a byte string: a sequence of bytes in the binary form. struct.unpack不需要字符串,而是字节字符串:二进制形式的字节序列。

 >>> data = "8a fd ff ff"
 >>> bytestr = "".join(chr(int(h, 16)) for h in data.split())
 >>> struct.unpack("<l", bytestr )
 (-630,)

Your array has a list of the string representations characters instead: 您的数组具有字符串表示形式字符的列表,而不是:

>>> import numpy as np
>>> data = "8a fd ff ff"
>>> aa = np.array(list(data))>>> aa
array(['8', 'a', ' ', 'f', 'd', ' ', 'f', 'f', ' ', 'f', 'f'],
      dtype='|S1')

It should contain the bytes themselves: 它应该包含字节本身:

>>> aa = np.array([chr(int(h, 16)) for h in data.split()])
>>> aa
array(['\x8a', '\xfd', '\xff', '\xff'],
      dtype='|S1')
>>> struct.unpack("<l", aa)
(-630,)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM