string.atoi and string.atol broken?

Peter Otten __peter__ at web.de
Wed Jan 26 12:11:06 CET 2005


Peter Otten wrote:

> def itoa(n, base):
>      assert 2 <= base <= 16
>      if n < 0:
>              digits = ["-"]
>              n = -n
>      else:
>              digits = []
>      while n:
>              n, m = divmod(n, base)
>              digits.append(string.hexdigits[m])
>      digits.reverse()
>      return "".join(digits)

This is junk, sorry. Doesn't handle n<=0 correctly (at least).

Peter




More information about the Python-list mailing list