0

I am trying to read a binary file (only 32 bits long) into a boolean array. It worked just fine when I followed:

Using Python How can I read the bits in a byte?

However, when I try to use the struct package I get a different results:

_fmt = '32?'

with open(filename, mode='rb') as fp:
    content = struct.unpack(fmt, fp.read(struct.calcsize(fmt))

I would like to understand why.

Thanks

Community
  • 1
  • 1
Hernan
  • 5,811
  • 10
  • 51
  • 86

2 Answers2

1

The format '32?' indicates 32 bytes, not 32 bits.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
0

Bitarray might help you: http://pypi.python.org/pypi/bitarray

Or the recipes here: http://wiki.python.org/moin/BitArrays

Marcin
  • 48,559
  • 18
  • 128
  • 201