binary representaion of a number

David Broadwell dbroadwell at mindspring.com
Mon Aug 21 01:00:16 EDT 2000


Neil Schemenauer wrote:
>
> import string
> 
> hexmap = {
>     "0": "0000",
>     "1": "0001",
>     "2": "0010",
>     "3": "0011",
>     "4": "0100",
>     "5": "0101",
>     "6": "0110",
>     "7": "0111",
>     "8": "1000",
>     "9": "1001",
>     "a": "1010",
>     "b": "1011",
>     "c": "1100",
>     "d": "1101",
>     "e": "1110",
>     "f": "1111",
> }
> 
> def bin(n):
>     bits = []
>     for digit in ("%0.8x" % n):
>         bits.append(hexmap[digit])
>     return string.join(bits, '')

Straitforward, I believe almost precisely what I was looking for.
Thank you.

Now, with the 'for digit in ("%0.8x" % n):' statement I am haveing
some difficulty understanding the '("%0.8x" % n)' notaion ...



More information about the Python-list mailing list