Binary numbers

Guyon Morée guyon.moree at gmail.com
Wed Jun 8 11:38:59 CEST 2005


Don't know if this is what you mean, but:

Binary to decimal:

>>> bin_num = '100001011'
>>> int(bin_num, 2)
267

>>> def dec2bin(dec_number):
...     if dec_number == 0: return '0'
...     return (dec2bin(dec_number >> 1) + str(dec_number % 2))
...
>>> dec2bin(267)
'0100001011'

--
Guyon Morée
guyon.moreeNOSP at Mgmail.com
http://gumuz.looze.net




More information about the Python-list mailing list