convert integer to binary
Paul Rubin
http
Fri May 2 18:42:21 EDT 2003
ekranawetter at utanet.at (ekranawetter-piber) writes:
> Hi all,
>
> I couldn't find any function in Python to convert integers to binary
> and vice versa, for example:
>
> integer 16 = bin 10000, or
> integer 15 = bin 1111
> bin 11 = integer 3
Note this doesn't bother stripping leading zeros from the result:
def i2b(n):
hdigits = ('0000','0001','0010','0011','0100','0101','0110','0111',
'1000','1001','1010','1011','1100','1101','1110','1111')
a = [hdigits[int(d,16)] for d in hex(n)[2:]]
return ''.join(a)
More information about the Python-list
mailing list