Convert Hexadecimal to Decimal in Python

To convert a hexadecimal value to decimal in Python, use the built-in int() function. Pass the hex number as the first number and 16 as the second.


Hex to Dec with the Python int() Function

To demonstrate this, let's change 8AB into its decimal representation using the int() function:


print(int('8AB', 16))


The reason why we use 16 as the second argument is to let the function know that it is being handed a base 16 value to convert to a decimal.


Using ast.literal_eval() to Convert Hexadecimal to Decimal in Python

Another way of converting hex values to decimals is with the ast.literal_eval() function. Here is an example of how to use it:


from ast import literal_eval

result = literal_eval('0x8AB')



Make sure the hex value is prefixed with 0x or the literal_eval() parser will throw an error.