"Dick Moores" <[EMAIL PROTECTED]> wrote

>I was surprised to be unable to find a function in Python for
> converting ints from base10 to base2. Is there one?

The sidebar in my Using the OS topicv includes a function
to do this. It uses a simpler algorithm:

def bin(n):
   digits = {'0':'000','1':'001','2':'010','3':'011',
             '4':'100','5':'101','6':'110','7':'111'}
   octStr = "%o" % n # convert to octal string
   binStr = ''
   # convert octal digit to its binary equivalent
   for c in octStr: binStr += digits[c]
   return binStr> I wrote one, but have I reinvented the wheel again? 
(Even if I have,
> it was an interesting exercise for me.)

There are several versions of this using various techniques on
the net.

> I know some of you CS people won't like what I do with negative 
> ints,

And I'm not sure how mine handles negative ints - badly I suspect!


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to