Binary Header

Peter Hansen peter at engcorp.com
Thu Jun 19 10:24:46 EDT 2003


Benoit BESSE wrote:
> 
> How can I translate a list of int into binary. The struct.pack('!B',...)
> need only integer.
> I want to have a 8 bit value which represent by
> V (3 bit) = 1
> A (1 bit) = 0
> R (1 bit) = 0
> T (1 bit) = 0 or 1
> E (1 bit) = 0
> C (1 bit) = 0
> Can I pack (1,0,0,1,0,0) ?

You may find this recipe helpful: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/113799

It looks like you could do this (untested):

>>> val = bf()
>>> v = t = 1
>>> a = r = e = c = 0
>>> val[5:8], val[4:5], val[3:4], val[2:3], val[1:2], val[0:1] = v, a, r, t, e, c

then you would expect to see

>>> print hex(int(val))
0x24

If it's not quite the right interface: change it!  It's just a "recipe",
not a final solution.

-Peter




More information about the Python-list mailing list