binascii.unhexlify ... not clear about usage, and output

mensanator at aol.com mensanator at aol.com
Wed Jul 11 22:34:04 CEST 2007


On Jul 11, 1:38 pm, Marc 'BlackJack' Rintsch <bj_... at gmx.net> wrote:
> On Wed, 11 Jul 2007 10:46:23 -0700, mensana... at aol.com wrote:
> > You can with gmpy:
>
> >>>> import gmpy
> >>>> x = 0x0164
> >>>> s = gmpy.digits(x,2)      # convert to base 2
> >>>> y = '0'*(16-len(s)) + s   # pad to 16 bits
> >>>> y
> > '0000000101100100'
>
> For the padding I'd use the `zfill()` method.
>
> In [15]: a = 0x0164
>
> In [16]: gmpy.digits(a, 2).zfill(16)
> Out[16]: '0000000101100100'
>
> Ciao,
>         Marc 'BlackJack' Rintsch

Damn, I didn't know you could do that.
If only there was a built-in base 2 conversion.




More information about the Python-list mailing list