Python - Search for hex value marker and extract d

2019-08-27 20:53发布

I am new to python and have tried searching for help prior to posting.

I have binary file that contains a number of values I need to parse. Each value has a hex header of two bytes and a third byte that gives a size of the data in that record to parse. The following is an example:

\x76\x12\x0A\x08\x00\x00\x00\x00\x00\x00\x00\x00

The \x76\x12 is the record marker and \x0A is the number of bytes to be read next.

This data always has the two byte marker and a third byte size. However the data to be parsed is variable and the record marker increments as follows: \x76\x12 and \x77\x12 and so on until \x79\x12 where is starts again.

This is just example data for the use of this posting.

Many Thanks for any help or pointers.

2条回答
三岁会撩人
2楼-- · 2019-08-27 21:13

Is something like this what you want?

>>> b = b'\x76\x12\x0A\x08\x00\x00\x00\x00\x00\x00\x00\x00'
>>> from StringIO import StringIO
>>> io = StringIO(b)
>>> io.seek(0)
>>> io.read(2) #read 2 bytes, maybe validate?
'v\x12'
>>> import struct
>>> nbytes = struct.unpack('B',io.read(1))
>>> print nbytes
(10,)
>>> data = io.read(nbytes[0])
>>> data
'\x08\x00\x00\x00\x00\x00\x00\x00\x00'
查看更多
不美不萌又怎样
3楼-- · 2019-08-27 21:20

This will treat the data as a raw string (to ignore '\' escape character and split into a list

a = r"\x76\x12\x0A\x08\x00\x00\x00\x00\x00\x00\x00\x00".split('\\')
print a

output: ['', 'x76', 'x12', 'x0A', 'x08', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00']

You can then iterate through the values you are interested in and convert them to decimal if required:

for i in range(len(a[4:])): # cutting off records before index 4 here
    print int(str(a[i+4][1:]),16)
查看更多
登录 后发表回答