How to read an integer value from a binary file?

Olivier Elbaz olivier.elbaz at gmail.com
Tue May 3 13:46:17 EDT 2005


Hi,

I read a bmp file (binary file) and I want to extract an integer value
from it (size value).
The whole binary file is read inside a list "all_bmp_file".
I can read the "size" value with the following code:
for r in all_bmp_file[2:5]:
   n = struct.unpack('B',r)#B for unsigned char
   print n
Result:
(182,)
(47,)
(0,)
The problem is that I would like to read the 4 bytes (integer) as one
number like:
struct.unpack('i','\xb6/\x00\x00')
Result
(12214,)
Note that I can't use  struct.unpack('i',all_bmp_file[2:5]) inside my
program since I got a syntax error.

Could you help me to find a solution?
Thanks,
Olivier



More information about the Python-list mailing list