[Tutor] Re: How to convert an integer into a binary?

lumbricus@gmx.net lumbricus@gmx.net
Wed, 8 Aug 2001 15:58:46 +0200


On Wed, Aug 08, 2001 at 08:43:20AM -0500, Christopher Smith wrote:
> lumbricus@gmx.net wrote:
> 
> Thanks for the bit-operations approach!  (Watch out for the trivial z=0
> case, though, which should return e=[0] instead of [].)

Yes *duck* silly me *patsch*

It inspired me to 
> change the approach that I had taken when needing this function.  Here's
> what I came up with:
> 
> def d2b(numb,b=2):
> 	'''Returns a list containing the base b representation of 
> 	the decimal number, big end first.
> 	'''
> 	l=[numb]
> 	while l[0]>=b:
> 		l.insert(1,0)
> 		l[0],l[1]=divmod(l[0],b)
> 	return l
>

How beautyful :-)
(saved)

> 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 
> addition, though.
> 
> /c
> 

Greetings J"o! :-)

-- 
No matter how old a mother is, she watches her middle-aged children for
signs of improvement.
		-- Florida Scott-Maxwell