Extracting 3-byte integers

Scott David Daniels scott.daniels at acm.org
Tue Jun 27 02:57:50 CEST 2006

```Bob Greschke wrote:
> I have some binary data read from a file that is arranged like
>    <3-byte int> <3-byte int> <3-byte int> etc.
> The "ints" are big-endian and there are 169 of them.  Is there any clever
> way to convert these to regular Python ints other than (struct) unpack'ing
> them one at a time and doing the math?

Best way is with scipy (or Numeric or numarray), but for vanilla Python:

import array, itertools
ubytes = array.array('B')
sbytes = array.array('b')
ubytes.fromfile(binarysource, 169 * 3)
sbytes.fromstring(ubytes[::3].tostring())

result = array.array('i', (msb * 256 + mid) * 256 + lsb
for msb, mid, lsb
in itertools.izip(sbytes,
ubytes[1::3], ubytes[2::3])))

If you want to get fancy, you can replace the last statement with:

del ubytes[::3]
uhalf = array.array('H')
uhalf.fromstring(ubytes.tostring())
if array.array('H', [1]) != '\x00\x01': uhalf.byteswap()
result = array.array('i', (msb * 65536 + low for msb, low
in itertools.izip(sbytes, uhalf)))

That was fun.

--Scott David Daniels
scott.daniels at acm.org

```