itoa

Alex alex at somewhere.round.here
Mon Mar 13 19:18:57 EST 2000


> Is there an integer to ascii (itoa) function in Python? I need to
> convert integers into various different bases (but to binary, in
> particular).

I'm not aware of one, but it's easy to roll your own:

import string

def itoa (n, base = 2):
    output = []
    while n:
        lowest_digit = n % base
        output.append (str (lowest_digit))
        n = (n - lowest_digit) / base
    output.reverse ()
    return string.join (output, '')

if __name__ == '__main__':
    print itoa (17)

Alex.




More information about the Python-list mailing list