Conversion of 24bit binary to int

Mike C. Fletcher mcfletch at
Tue Nov 11 16:21:29 CET 2003

If I'm understanding correctly, hex has nothing to do with this and the 
data is really binary, so what you're looking for is probably:

 >>> data = '\000\001\002'
 >>> temp = struct.unpack( '>I', '\000'+data ) # pad to 4-byte unsigned 
big-endian integer format
 >>> print temp # is now a regular python integer (in a tuple)
 >>> print repr(struct.pack( '<I', *temp )) # encode in 4-byte unsigned 
little-endian integer format

There are faster ways if you have a lot of such data (e.g. PIL would 
likely have something to manipulate RGB to RGBA images), similarly, you 
could use Numpy to add large numbers of rows simultaneously (all 512 if 
I understand your description of the data correctly).  Without knowing 
what type of data is being loaded it's hard to give a better recommendation.


Idar wrote:

> Is there an effecient/fast way in python to convert binary data from 
> file (24bit hex(int) big endian) to 32bit int (little endian)? Have 
> seen struct.unpack, but I am unsure how and what Python has to offer. 
> Idar

  Mike C. Fletcher
  Designer, VR Plumber, Coder

More information about the Python-list mailing list