base converter

Fredrik Lundh fredrik at pythonware.com
Wed Jun 13 18:01:08 EDT 2001


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>





More information about the Python-list mailing list