On 27/06/2006 9:36 AM, 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? >
I'd call that "arithmetic", not "math" :-) Here's another way, not touted as "clever": |>> guff = "\x00\x00\x01\x00\x02\x01\x03\x00\x00\x00\x00\xFF" |>> expected = [1, 513, 3 * 65536, 255] |>> expected [1, 513, 196608, 255] |>> import array |>> b = array.array('B', guff) |>> b array('B', [0, 0, 1, 0, 2, 1, 3, 0, 0, 0, 0, 255]) |>> actual = [b[x]*65536 + b[x+1]*256 + b[x+2] for x in range(0, len(b), 3)] |>> actual [1, 513, 196608, 255] |>> actual == expected True Cheers, John -- http://mail.python.org/mailman/listinfo/python-list