string.atoi and string.atol broken?
Dan Bishop
danb_83 at yahoo.com
Tue Jan 25 19:46:57 EST 2005
Peter Otten wrote:
> Mike Moum wrote:
>
> > s.atoi('4',3) should result in 11
> >
> > s.atoi('13',4) should result in 31
> >
> > s.atoi('12',4) should result in 30
> >
> > s.atoi('8',4) is legitimate, but it generates an error.
> >
> > Is this a bug, or am I missing something obvious?
>
> You and atoi() seem to disagree about the direction of the
conversion, and
> atoi() wins :-). It converts a string representation of a number into
the
> corresponding integer. The second parameter specifies in what base
this
> string is given.
> You seem to want something like
>
> import string
>
> def itoa(n, base):
> assert 2 <= base <= 16
Why have the restriction base <= 16? int() allows up to 36. All you
need to do is
BASE36_DIGITS = string.digits + string.lowercase
and change
> digits.append(string.hexdigits[m])
to
> digits.append(BASE36_DIGITS[m])
More information about the Python-list
mailing list