In this article, we will learn to convert hex strings to int in Python Programming language.
Table Of Contents
What we know about hex strings ?
Hexadecimal Number String are Hex numbering system, which uses Base of 16 system. In Simple words it is a combination of alphabets and integers or may be called as Alphanumeric.
Lets see the methods through which we can convert hex strings to int with the help of Python Programming Language. Also I have used Python 3.10.1 for writing example codes. To check your version write python –version in your terminal.
Convert hex string to int using ast.literal_eval()
ast stands for Abstract Syntax Tree which is a built in module of Python. We will use a helper function of ast module which is literal_eval() function to convert hex strings to int.
This method is used to traverse an abstract syntax tree. It recieves only one parameter i.e. a variable of string or any other data type which needs to be evaluated. Lets see an Example of this method.
import ast hexstring_var = "0xa" # this will print the data type of hexstring_var print(type(hexstring_var)) # we have used literal_eval() function to convert hex to int int_var = ast.literal_eval(hexstring_var) # this will print the data type of int_var print(type(int_var)) print(int_var)
<class 'str'> <class 'int'> 10
You can see by using literal.eval() method of ast module, we have successfully converted a hex string to int. You can see the data type of hexstring_var is of type class str. Whereas, data type of int_var in which converted value of hex string has been stored is of type class int.
Latest Python - Video Tutorial
Convert hex string to int using int() method
Next method that we will be using to accomplish our job is int() method. This is also the most used and effective method to convert hex to an integer.
This function recieves two parameters :
- First is the hex string which needs to be converted.
- Second is the base of number format which is also a optional parameter and its default value is 10.
There are also other number formats like 2 for binary , 8 for octal , 16 for hexadecimal. Now lets see an example of this method.
hexstring_var = "0xa" # this will print the data type of hexstring_var print(type(hexstring_var)) int_var = int(hexstring_var,16) # this will print the data type of int_var print(type(int_var)) print(int_var)
<class 'str'> <class 'int'> 10
In code above you can see by using int() method we have converted a hex string to an integer. Here I have provided 16 as a second argument because if the hex string has a prefix of 0x then it is called a Prefixed Hex String and if we do not provide any base number as a second argument it will throw a ValueError. So, we need to pass 0 or 16 as a second argument. It can also work with Non-Prefixed Hex String, the hex string which doesn’t has 0x as a prefix.
So in this article we learned to convert hex string to int using two different methods, which are int() method and literal_eval() method of ast module. We also learned about hex strings which are Prefixed Hex String and Non-Prefxied Hex String, there are also other types of hex strings which are of Little and Big Endian Hex String. We must use int() method with correct base value to avoid getting ValueError. Read the article and make sure to understand the code and play along with different types of hex strings and with suitable base value. Run these codes on your machine.
Latest Video Tutorials