Reading a Bitstream

Dietrich Epp dietrich at zdome.net
Wed Nov 19 04:47:26 EST 2003


On Nov 18, 2003, at 6:10 PM, Patrick Maupin wrote:

> Dietrich Epp wrote:
>
>> Are there any good modules for reading a bitstream?  Specifically, I
>> have a string and I want to be able to get the next N bits as an
>> integer.  Right now I'm using struct.unpack and bit operations, it's a
>> bit kludgy but it gets the right results.
>
> As Miki wrote, the array module will probably give you what
> you want more easily than struct.unpack.  If you need more
> help, just post a few more details and I will post a code
> snippet.  (As to the rest of Miki's post, I'm not sure that
> I really want to know what an "Upnacker" is :)

Maybe I should clarify: I need to read bit fields.  Neither are they 
aligned to bytes or do they have fixed offsets.  In fact, in one part 
of the file there is a list of objects which starts with a 9 bit object 
type followed by fields whose length and number depend on that object 
type, ranging from a dummy 1-bit field to a tuple of four fields of 
length 9, 5, 8, and 8 bits.

I looked at the array module and can't find what I'm looking for.  
Here's a bit of typical usage.

def readStuff(bytes):
   bits = BitStream(bytes[2:])
   isSimple = bits.Get(1)
   objType = chr(bits.Get(8))
   objType += chr(bits.Get(8))
   objType += chr(bits.Get(8))
   objType += chr(bits.Get(8))
   count = bits.Get(3)
   bits.Ignore(5)
   if not isSimple:
     objId = bits.Get(32)
   bytes = bytes[2+bits.PartialBytesRead():]
   return bytes, objType

This is basically the gamut of what I want to do.  I have a string, and 
create a bit stream object.  I read fields from the bit stream, some 
may not be present, then return an object and the string that comes 
after it.  The objects are aligned to bytes in this case even though 
their fields aren't.

I can't figure out how to get array to do this.  Array does not look at 
all suited to reading a bit stream.  struct.unpack *does* work right 
now, with a lot of help, I was wondering if there was an easier way. 
  






More information about the Python-list mailing list