Binary Header

Benoit BESSE benoit.besse at club-internet.fr
Thu Jun 19 15:34:54 EDT 2003


Thanks a lots, yopu're right but you have to change
class bf(object): to class bf:

BB

"Peter Hansen" <peter at engcorp.com> a écrit dans le message de
news:3EF1C7AE.DC43ECF6 at engcorp.com...
> 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