On Wed, 10 Oct 2007 03:42:15 -0700, John Machin wrote: > On Oct 10, 8:15 pm, "Guilherme Polo" <[EMAIL PROTECTED]> wrote: >> 2007/10/10, cprogrammer <[EMAIL PROTECTED]>: >> > 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/ -- http://mail.python.org/mailman/listinfo/python-list