Python hex to int

Python hex to int

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.

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:

Output:

Initial String: F
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:

Output:

296

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.

Output:

ValueError Traceback (most recent call last)
in
—-> 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.

Output:

296

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:

Output:

Initial hexadecimal string: 0x128
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:

Output:

Enter initial hexadecimal string: D3
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.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *