struct.unpack less than 1 byte
Stargaming
stargaming at gmail.com
Wed Oct 10 10:00:40 EDT 2007
On Wed, 10 Oct 2007 03:42:15 -0700, John Machin wrote:
> On Oct 10, 8:15 pm, "Guilherme Polo" <ggp... at gmail.com> wrote:
>> 2007/10/10, cprogrammer <cprogram... at gmail.com>:
>> > i need to read from a file a struct like this [1byte, 12bits, 12bits]
>> > reading 1 byte or more is not a problem ... but the 12 bits values
>> > are ...
>>
>> 12bits, 12bits == 3 byes
>
> and 8 + 12 + 12 = 32 :-)
[snipped bitfiddling]
Or, if you're doing more of those parsing tasks (or just dislike bit
shifting), you could have a look into `construct`_::
>>> from construct import *
>>> foo = BitStruct("foo",
... Octet("spam"), # synonym for BitField("spam", 8)
... BitField("ham", 12),
... BitField("eggs", 12)
... )
>>> foo
Buffered('foo')
>>> foo.parse("\xff\xff\xff\xff")
Container(eggs = 4095, ham = 4095, spam = 255)
Cheers,
Stargaming
.. _construct: http://construct.wikispaces.com/
More information about the Python-list
mailing list