Can string formatting be used to convert an integer to its binary form ?

Mirco Wahab peace.is.our.profession at gmx.de
Thu Sep 28 04:22:51 EDT 2006


Thus spoke fdu.xiaojf at gmail.com (on 2006-09-28 09:10):

> String formatting can be used to converting an integer to its octal or 
> hexadecimal form:
>  >>> a = 199
>  >>> "%o" % a
> '307'
>  >>> "%x" % a
> 'c7'
> 
> But, can string formatting be used to convert an integer to its binary 
> form ?

I didn't fell over this problem so far but
I *would* have expected to find something
like a 'pack' operator (as in Perl).

And voilá, there is (even basically identical to Perl):

  from struct import *

  a = 199
  a_bin_str = pack('L', a)



Regards

Mirco



More information about the Python-list mailing list