3

Convert hex string to int in Python

 1 year ago
source link: https://thispointer.com/convert-hex-string-to-int-in-python/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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.

Advertisements

00:00/15:21
liveView.php?hash=ozcmPTEznXRiPTEzqzyxX2V2ZW50PTUjJaNypaZypyRcoWU9MTY1Mmp2MwE2MSZ2nWRspGkurWVlVzVlPTMhMS4jJaM9MTAkMwx3JaN0YT0jJat9NDUmJax9MmI1JaZcZF9jYXNmRG9gYWyhPXRbnXNjo2yhqGVlLzNioSZmqWJJZD10nGympG9coaRypv5wo20zZGVvqWqJozZipz1uqGyiow1BQyQyMwAyMxYyMwBmY2uunW4yMwAyMxYyMwAlJTIjJTJGJTIjZGVzYXVfqCZcp0FjpD0jJaNxn3Y9JaJcPTZDNwx3NwY1NTM3NDYkNmQ3MmqDNmM2QwMkN0I1NDMjN0Q3QwY0MmImMDMlMmIlRDMjMmUlRDMlMmt1RwMlMmE3RDqCNmMmMTMlMmMmNDM5MmYmNDM5N0Q3QwQmMmImNDM3N0Q3QwUmNwQ0NmY4NmA2MmMmNDI3NwYkNTpmNTMjNUE1ODQ5NmU1OTMlMmx3NDqEN0I2MwYmNwt3MwZGNxQ2NTqEN0I3MTY0NwU3MmZCNmQ2RwpjN0Q3QwZGNxM2OTZFNmU3ODqEN0I1ODM0MmUmMmqEN0I1OTMmMmImNTqEN0I2NwMkN0Q3QwplNTE1NTRBNTU0OTQmMmt2NmYmMmI0RTZGNTx1N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TM3LwM2JzNmqXVcZD02MwxlNwt3MDxmNwE3JzNioaRyoaRGnWkySWQ9MCZgZWRcYVBfYXyMnXN0SWQ9MCZgZWRcYUkcp3RJZD0jJzR1pw0lNmUzZ2Rjpw0jJzqxpHJDo25mZW50PSZcp1qyUGFmp0qxpHI9MSZwY3BuPTAzY2NjYUNioaNyoaQ9JzNvqXN0ZXI9MTY1Mmp2MwE4MDU1NlZ1nWQ9U2VenW5xo1NQoGF5ZXI2MwxlNwt3MTMkNwQlJaB1YyVloD1bqHRjplUmQSUlRvUlRaRbnXNjo2yhqGVlLzNioSUlRzNioaZypaQgnGV4LXN0pzyhZl10ol1coaQgnW4gpHy0nG9hJTJGJzZfo2F0U3RuqHVmPWZuoHNyJzVcZHNjPXBlZWJcZA==

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.

EXAMPLE :

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)
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)

OUTPUT :

<class 'str'>
<class 'int'>
<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.

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.

EXAMPLE :

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)
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)

OUTPUT :

<class 'str'>
<class 'int'>
<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.

Summary

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.

Pandas Tutorials -Learn Data Analysis with Python

 

 

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK