struct.unpack() and bit operations

mikeSpindler mike at spindler.us
Thu Sep 23 18:02:43 CEST 2004


THANKS FOR THE HELP ON MY LAST INQUIRY!  AWESOME ANSWERS.

I am reading in from a binary file data that is formatted as 32 *
16-bit words.  So when I read it in I'm apparently not getting or
addressing correctly the upper 16-bits (since I have no choice but
read 32-bit elements - right?).

Is there an easier way to either get 16-bit "unpacks" or grab the
upper 16-bits with success?

Here's what I'm trying:

mask0 = 1111111100000000
mask1 = 0000000011111111

pdr32 = struct.unpack('%si' % (itemsize/4), pdrData.read(itemsize))

while i < 32
    pdr16[x] = (pdr32[y] & (mask0 / (2**8)))
    pdr16[x+1] = (pdr32[y] & mask1)
    x += 2
    y += 1
    pdrData.seek(32,1)


Thank you!
Mike Spindler



More information about the Python-list mailing list