Conversion of 24bit binary to int

Idar ip at itk.ntnu.no
Wed Nov 12 11:01:01 CET 2003


On Tue, 11 Nov 2003 10:21:29 -0500, Mike C. Fletcher <mcfletch at rogers.com> 
wrote:

> 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:

Thanks for the hint!! and sorry - i ment binary!
>
> >>> 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.

It is binary with no formating characters to indicate start/end of each 
block (fixed size).
A file is about 6MB (and about 300 of them again...),
Ch1: 1536B (512*3B) - the 3B are big endian (int)
..
Ch6: 1536B (512*3B)
And then it is repeated till the end:
Ch1: 1536B (512*3B)
..
Ch6: 1536B (512*3B)

ciao, idar

>
> 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/
>
>
>
>
>



-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/




More information about the Python-list mailing list