James James - 8 months ago 53
Python Question

spliting Hex data using Python

I have sets of this data : 0018f3aaaaa955554e0000 (00 18 f3 ... each being a byte)

I am trying to get 4th,5th,6th and 7th,8th,9th octets by seperating into

00 18 f3 aa aa a9 55 55 4e 00 00 then get aaaaa9, 55554e.

What would be best way to achieve this using python?

Answer Source

You can use the decode('hex') and encode('hex') string methods to accomplish this:

>>> hex_bytes = '0018f3aaaaa955554e0000'
>>> data = hex_bytes.decode('hex')
>>> data[3:6].encode('hex')
>>> data[6:9].encode('hex')