[Tutor] First real script

Hieu Hoang dhhoa1 at student.monash.edu
Thu Nov 9 03:55:23 CET 2006


Hi Carlos,
I hope this module would help you with the binary conversion part:

<pycode>
def tobits(inte,size = 3):
    """Copy an integer's bits from memory"""
    s=''
    for i in range(size):
        s += str((inte & (1<<i)) >>i)
    #bits are extracted right-to-left
    s = s[::-1] #reverse the result string
    print s
    return list(s)
</pycode>

<pyshell>
>>>test = tobits(30,8)
00011110
>>>test
['0', '0', '0', '1', '1', '1', '1', '0']
</pyshell>

Cheers,
Rooy


More information about the Tutor mailing list