int() should be extended?
fredrik at pythonware.com
Mon Oct 21 20:55:41 CEST 2002
Xiao-Qin Xia wrote:
> int() can convert some string into integer, except a string from hex():
> >>> int("41")
> >>> int("041")
> >>> int("0x41")
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> ValueError: invalid literal for int(): 0x41
> >>> exec "c = 0x41"
> >>> c
> since use exec is not so good a habit, is there any other way to convert
> "0x41" to 65 (0x41)?, or int() should be extened to do this job?
it's already extended:
Convert a string or number to a plain integer. If the
argument is a string, it must contain a possibly signed
decimal number representable as a Python integer,
possibly embedded in whitespace; this behaves
identical to string.atoi(x[, radix]). The radix
parameter gives the base for the conversion and
may be any integer in the range [2, 36], or zero.
If radix is zero, the proper radix is guessed based
on the contents of string; the interpretation is the
same as for integer literals."
More information about the Python-list