Conversion of 24bit binary to int

Mike C. Fletcher mcfletch at rogers.com
Tue Nov 11 10:21:29 EST 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)
(258L,)
 >>> print repr(struct.pack( '<I', *temp )) # encode in 4-byte unsigned 
little-endian integer format
'\x02\x01\x00\x00'

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.

HTH,
Mike


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
  http://members.rogers.com/mcfletch/








More information about the Python-list mailing list