[Tutor] Re: How to convert an integer into a binary?
Wed, 08 Aug 2001 08:43:20 -0500
| On Wed, Aug 08, 2001 at 03:36:04PM +0900, Haiyang wrote:
| > How can I convert an interger into a binary code in Python?
| This should become a faq ;-)
| import sys
| > "))
| print "Need a number."
| while (z > 0):
| if (z & 1) == 1:
| print e
Thanks for the bit-operations approach! (Watch out for the trivial z=0
case, though, which should return e= instead of .) It inspired me to
change the approach that I had taken when needing this function. Here's
what I came up with:
'''Returns a list containing the base b representation of
the decimal number, big end first.
There's already a version on the Useless site. Coupling
what you had (case for b==2) with the above might make for a good