Conversion of 24bit binary to int

Idar ip at
Wed Nov 12 11:38:12 CET 2003

Thanks for the example!

The format 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...), so

Ch1: 1536B (512*3B) - the 3B are big endian (int)
Ch6: 1536B (512*3B)
And then it is repeated till the end (say Y sets of Ch1 (the same for 
Ch1,Y: 1536B (512*3B)
Ch6,Y: 1536B (512*3B)

And idealy I would like to convert it to this format:
Ch1: Y*512*4B (normal int with little endian)
And that is the end :)

> This code assumes that you have a string (named teststr here)
> in the source format you describe.  You can get a string
> like this in several ways, e.g. by reading from a file object.
> This code then swaps every 3 characters and inserts a null
> byte between every group of three characters.
> The result is in a list, which can easily be converted back
> to a string by ''.join() as shown in the test printout.
> I would expect that either the array module or Numpy would
> work faster with _exactly_ the same technique, but I'm
> not bored enough to check that out right now.
> If this isn't fast enough after using array or NumPy (or
> after Alex, Tim, et al. get through with it), I would
> highly recommend Pyrex -- you can do exactly the same
> sorts of coercions you were doing in your C++ code.
> teststr = ''.join([chr(i) for i in range(128,128+20*3)])
> result = len(teststr) * 4 // 3 * [chr(0)]
> for x in range(3):
> result[2-x::4] = teststr[x::3]
> print repr(''.join(result))
> Regards,
> Pat

Using M2, Opera's revolutionary e-mail client:

More information about the Python-list mailing list