int 2 binary: What do you think?

Martin Bless m.bless at gmx.de
Sun May 13 15:56:27 EDT 2001


[Andrew Gaul]

>Here's a shorter implementation; it may be more efficient:

I bet it is, very nice. I see, hex() is very helpful.
And having map() running along a string bilding a list of strings -
I still have to learn what can be accomplished by combining simple
techniques in Python ...

>def bin(i):
>    l = ['0000', '0001', '0010', '0011', '0100', '0101', '0110', '0111',
>         '1000', '1001', '1010', '1011', '1100', '1101', '1110', '1111']
>    s = ''.join(map(lambda x, l=l: l[int(x, 16)], hex(i)[2:]))
>    if s[0] == '1' and i > 0:
>        s = '0000' + s
>    return s

bin() is a good name - considering the existing hex(). I'll keep this
version.

>It'd be nice if an equivalent built-in function was added in a future
>version of Python.

Yes.

Martin




More information about the Python-list mailing list