Computer >> Computer tutorials >  >> Programming >> Python

How to convert hex string into int in Python?


Hex strings generally have a "0x" prefix. If you have this prefix and a valid string, you can use int(string, 0) to get the integer. The 0 is provided to tell the function to automatically interpret the base from prefix. For example:

>>> int("0xfe43", 0)
65091

If you don't have a "0x" prefix, you can pass 16 instead of 0 to specify the base of the number. For example:

>>> int("fe43", 16)
65091