Inverse of int(s, base)?

olli at secnetix.de olli at secnetix.de
Mon May 10 17:57:55 EDT 2004


Hi,

Is there an inverse function of int(s, base) where base
can be any number up to 36?  I've searched the library
reference up and down, but haven't found anything.

For example, I need to work with base-24 numbers, which
can be parsed nicely with x = int(s, 24), but there
doesn't seem to be a way to convert integers back to
their base-24 representation as a string.  (Of course,
I can define my own function in Python to do that, but
I wonder if there's a more efficient way.)

Best regards
   Oliver

PS:  This is what I'm using right now.

import string
str_digits = string.digits + string.ascii_lowercase

def str_base (x, base):
        result = ""
        while x:
                result = str_digits[x % base] + result
                x /= base
        return result or "0"

>>> print str_base(2065027084, 24)
aj83kb4
>>> int("aj83kb4", 24)
2065027084

-- 
Oliver Fromme, secnetix GmbH & Co KG, Oettingenstr. 2, 80538 München
Any opinions expressed in this message may be personal to the author
and may not necessarily reflect the opinions of secnetix in any way.

"Clear perl code is better than unclear awk code; but NOTHING
comes close to unclear perl code"  (taken from comp.lang.awk FAQ)



More information about the Python-list mailing list