How do you convert a hex value in a string to an int?

Alex Martelli aleax at aleax.it
Mon Feb 10 17:19:46 EST 2003


Grumfish wrote:

> How do you convert a hex value in a string to an int?

call int with two arguments:

>>> int('0x23', 0)
35
>>> int('23', 16)
35
>>> int('0x23', 16)
35
>>>

a 2nd argument of 0 works only if the string is in the
proper format, such as 0x23.  A 2nd argument of 16 also
works for strings missing the leading '0x'.


Alex





More information about the Python-list mailing list