Convert Hex String to Int in Python

To convert a hex string to an int in Python, use the int() function. Pass the hex string as the first argument and 16 as the second to tell int() that you are converting a base 16 value.

 

Hex String to Int Python Example

The example below demonstrates how to convert a base 16 string to an integer using the Python int() function.

 

res = int("c0ffee", 16)

print(res)
12648430

 

Convert Prefixed Hex String to Int in Python

If you are converting various prefixed values it is best to supply 0 as the second argument of the int() function. This tells it to automatically detect the type of data supplied.

 

res = int("0x000C", 0)

print(res)
12