[Tutor] to/from binary to/from integer (i.e.'01010101' =85)

kiran at mhowlinux.org kiran at mhowlinux.org
Tue Jan 13 07:23:23 EST 2004


for any positive (int) to binary

a simple function would do the job

import math
def i2b(s):
   #check how many bits are required to represent the integer in binary
    i=int(math.log(s)/math.log(2))+1
    return ''.join([str((s&(2**(i-j-1)))>>(i-j-1)) for j in range(i)])



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.554 / Virus Database: 346 - Release Date: 12/20/2003



More information about the Tutor mailing list