views:

47

answers:

1
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?

+2  A: 
>>> 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