In the world of programming, there are a lot of conversions of data types that programmers have to make that best suit their problem statement. One of these conversions is a hexadecimal string to integer and vice-versa. The hexadecimal number is one such number system that is highly used in storing memory addresses, IP addresses, and more. And integers in python are used in pretty much every program of any programming language.

`Hexadecimal`

values have a base of 16. In Python, hexadecimal strings are prefixed with `0x`

. Integer values are of many types, they can be whole numbers, positive or negative numbers, floating-point numbers,etc.

In this tutorial, we will see some methods to convert **hex String to int** in Python.

Table of Contents

## Use of `int()`

Function to convert hex to int in Python

`int()`

function is one of the most common functions to convert hex to int in python. This function helps us to convert a specific number in its present base to a decimal value. While using this function, the base of the number that has to be converted must be mentioned.

This function also helps in converting alphabets related to the hexadecimal system that are from A to F.

Example:

1 2 3 4 5 6 |
initial_string = 'F' print("Initial String: " + str(initial_string)) conversion = int(initial_string, base=16) print("Conversion of hexadecimal string : " + str(conversion)) |

Output:

Conversion of hexadecimal string : 15

Also, note that `str()`

function is used to concatenate the converted integer with the given string.

To convert a hexadecimal number to a normal integer using the `int()`

function, the following is the way to do it:

1 2 3 |
print(int('0x128', base=16)) |

Output:

Note that `0x`

is used as a prefix of the hexadecimal number that has to be converted i.e 24. `0x`

prefix denotes the number given by the user is a hexadecimal number. Here, it was not necessary to write the `0x`

prefix to denote the hexadecimal number as the base of the number is also mentioned.

However, if the `0x`

prefix is mentioned and the base is not defined, there will be an ValueError: invalid literal for int() with base 10.

1 2 3 |
print(int('0x128')) |

Output:

—-> 1 int(‘0x128’)

ValueError: invalid literal for int() with base 10: ‘0x128’

However you can use base as `0`

if you have `0x`

as prefix in hexadecimal string.

1 2 3 |
print(int('0x128',0)) |

**Output:**

## Use of `ast.literal_eval()`

Function to convert hex to int in Python

`ast`

stands for Abstract Syntax Trees which an in-built python module used for finding out the current grammar in a very systematic manner.

The `ast.literal_eval()`

function helps in evaluating python expressions and python structures like string, floats, integers, tuples, lists, dictionaries, etc.

Example:

1 2 3 4 5 6 7 |
from ast import literal_eval initial_string = '0x128' conversion = literal_eval(initial_string) print("Initial hexadecimal string: ", initial_string) print("Conversion of hexadecimal string: ", conversion) |

Output:

Conversion of hexadecimal string: 296

Here, the `literal_eval()`

function is applied to the initial string. After that, the `literal_eval()`

function finds the base of that string and converts the number to its integer value. Finally, both initial values and the hexadecimal values are printed.

## Use of Dictionary to convert hex to int in Python

Dictionary in python is a collection of items that have their individual values. To get a value of a particular item, that item is called with its name.

In this method, we will make a dictionary consisting of values that will help us in the conversion of hexadecimal string to an integer.

Example:

1 2 3 4 5 6 7 8 9 10 |
conversion_table = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, 'A': 10 , 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15} input_hex = input("Enter initial hexadecimal string: ").strip().upper() integer=0 length = len(input_hex) -1 for number in input_hex: integer += conversion_table[number]*16**length length -= 1 print("Conversion of hexadecimal string : ",integer) |

Output:

Conversion of hexadecimal string : 211

Note that in this method, we have taken input from the user and we have used the `upper()`

function to convert the input to upper-case. After that, we have initialized the integer value to 0 and used a for loop for the conversion. The `len()`

function used in the above code is used to get the number of characters in the input string. Finally, we print the converted value.

That’s all about Python hex to int.