base converter

Rainy sill at optonline.net
Wed Jun 13 18:57:27 EDT 2001


On Wed, 13 Jun 2001 22:01:08 GMT, Fredrik Lundh <fredrik at pythonware.com> wrote:
> Jeroen Wolff wrote:
>> I looked on the net for a baseconverter written in python. Is there
>> any? Something like:
>>
>> BaseConvert(20,2) --> "10100"
> 
> def BaseConvert(x, b):
>     out = ""
>     while x:
>         x, d = divmod(x, b)
>         out = str(d) + out
>     return out
> 
> (why has decimal to binary conversion suddenly turned into a
> FAQ the last few months?  it wasn't this way in the old days,
> and I cannot remember ever having to output things as binary
> numbers in a real-life project...  can anyone explain?)
> 
> </F>

Probably because many tutorials/books at least mention binary
and ask you to try converting a few numbers to binary. Newbies
read the book, try to convert, then try to confirm with python,
hence the questions.

> 
> 


-- 
Shine on you crazy diamond
        - Roger



More information about the Python-list mailing list