faqts : Computers : Programming : Languages : Python : Common Problems

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

39 of 43 people (91%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

How can I convert a hex number to an integer?

Jun 20th, 2000 19:40
unknown unknown, Jarkko Torppa, Thomas Wouters


>>> eval('0x32')
50
>>> import string
# Forces base to 16(hex)
>>> string.atoi('32',16)
50
# Guesses base from leading characters
>>> string.atoi('0x32',0)

Note that in Python 1.6, you can do the above two things like this:

>>> int('0x32',16)
50
>>> int('0x32',0)
50