0
import binascii

f = open('file.ext', 'rb')
print binascii.hexlify(f.read(4))
f.close()

This prints:

84010100

I know that I must retrieve the hex number 184 out of this data. How can it be done in python? I've used the struct module before, but I don't know if its little endian, big..whatever.. how can I get 184 from this number using struct?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
springwater4u
  • 31
  • 1
  • 3

1 Answers1

2
>>> x = b'\x84\x01\x01\x00'
>>> import struct
>>> struct.unpack_from('<h', x)
(388,)
>>> map(hex, struct.unpack_from('<h', x))
['0x184']

< means little endian, h means read a 16-bit integer ("short"). Detail is in the package doc.

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005