Convert Hex String to Int in Python

To represent a hexadecimal number in string format, the hexadecimal string is used in Python. In mathematics, all numbers having base-16 are represented as hexadecimal numbers. These numbers are from the digits 0-9 and the letters A-F (or a-f). To convert these numbers into integers, various methods are used in Python.

This post will explain different methods that assist the users in converting the hex string to an integer in Python:

Method 1: Using int() Method

The “int()” function converts different data types into the integer data type in Python. The Hexadecimal numbers are commonly prefixed with “0x” to show that they are hexadecimal strings. For instance, the hexadecimal string ‘0x2f’ represents the decimal value 47. In the example given below, the non-prefixed hex value is converted into an integer:

Example 1: Using Non-Prefixed Hex String

The non-prefixed hex value is the value that does not start with “0x” in Python. The base value “16” is compulsory to pass as an argument to the “int()” function. This is because if we don’t pass the base “16”, the “int()” function does not detect the hexadecimal number automatically. Let’s understand this example via the following code:


string_hex = "ABCD"
output = int(string_hex, 16)
print("Integer Value: ", output)

In the above code, the hex value “ABCD” is passed as a parameter value to the “int()” function. The “int()” function returns the integer value of the input hex string.


The above output verified the conversion of the hex string to int.

Example 2: Using Prefixed Hex String

If the input hex string value starts with “0x”, then the base of the “int()” function is not necessary to pass. We can pass “0” as an argument to the “int()” function, and it will detect the given hex string automatically. The below code shows how we can do that in Python:


string_hex = '0x42'
print(int(string_hex, 0))
print(int(string_hex, 16))

In the above code, the hex string is passed using the prefix “0x42”. The “int()” function having base 0 and 16 returns the same result because the function automatically detects the hex string value that is passed using the prefix “0x”.


The above output verified the conversion of the hex string to int.

Method 2: Using literal_eval() Method

The built-in module named Abstract Syntax Tree “ast” provides the “ast.literal_eval()” function that is used to convert the hex string into an integer. Here is an example of this code:


import ast
string_hex = "0xfe00"
output = ast.literal_eval(string_hex)
print("Integer Value: ", output)

Firstly, the “ast” module is imported in the program, and after that “ast.literal_eval()” function is used to convert the prefix hex string value into an integer.


The output shows the conversion of the hex string into an integer using “ast.literal_eval()”.


To convert hex string to integer, the built-in “int()” function and the “ast.literal_eval()” function of the “ast” module is used in Python. The “int()” function converts prefixed and non-prefixed hex strings into integers. This article covered all methods used to convert hex strings into integers using various examples.