int() should be extended?

Fredrik Lundh fredrik at pythonware.com
Mon Oct 21 14:55:41 EDT 2002


Xiao-Qin Xia wrote:

> int() can convert some string into integer, except a string from hex():
> """
> >>> int("41")
> 41
> >>> int("041")
> 41
> >>> int("0x41")
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> ValueError: invalid literal for int(): 0x41
> >>>
> >>> exec "c = 0x41"
> >>> c
> 65
> """
>
> 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:

http://www.python.org/doc/current/lib/built-in-funcs.html

    "int(x[, radix])

    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."

</F>





More information about the Python-list mailing list