Python Hex String to Integer List
Last Updated :
12 Mar, 2024
Hexadecimal strings are commonly encountered when working with low-level programming, networking, or cryptographic applications. In Python, converting a hex string to an integer array or list is a frequent task. In this article, we'll explore some different methods to achieve this conversion.
Hex String To Integer Array
We change a Hex string into a list of numbers, where every 2 hex values make one number. Look at the picture below to see how this change happens. We group 4 bits from each hex to create an 8-bit number. These numbers can range from 0 to 255.

Python Hex String To Integer Array Or List
Below, are the ways to convert Python Hex String To Integer Array Or List.
Python Hex String To Integer Array Or List Using map()
In this example, below code transforms the hex string "1a2b3c4d" into an integer array using a lambda function and map(). It prints the original hex string and the resulting integer array, but there's a typo in the print statement; it should be corrected to "integerArray."
Python3
hexString = "1a2b3c4d"
intArray = list(map(lambda x: int(x, 16),
[hexString[i:i+2] for i in range(0, len(hexString), 2)]))
print("Hex String: ",hexString)
print("Integer array: ",intArray)
OutputHex String: 1a2b3c4d
Integer array: [26, 43, 60, 77]
Python Hex String To Integer Array Or List Using List Comprehension
In this example, below code converts the hex string '48E59C7' into an integer array, where each pair of hex values corresponds to an integer. It then prints both the original hex string and the resulting integer array.
Python3
hexString = '48E59C7'
intArray = [int(hexString[i:i+2], 16) for i in range(0, len(hexString), 2)]
print("Hex String: ",hexString)
print("Integer array: ",intArray)
OutputHex String: 48E59C7
Integer array: [72, 229, 156, 7]
Python Hex String To Integer Array Or List Using bytes.from( )
In this example, below code transforms the hex string '48E59C' into an integer array using the `bytes.fromhex()` method. The resulting integer array is then printed, along with the original hex string.
Python3
hexString = '48E59C'
intArray = list(bytes.fromhex(hexString))
print("Hex String: ",hexString)
print("Integer array: ",intArray)
OutputHex String: 48E59C
Integer array: [72, 229, 156]
Conclusion
In conclusion, converting a hex string to an integer array or list in Python can be accomplished through various methods. Whether using list comprehension, map(), bytearray.fromhex(), or struct.unpack(), each approach provides a distinct way to handle the conversion. The choice of method may depend on factors such as readability, performance, or specific project requirements.
Similar Reads
Convert Hex String To Integer in Python Hexadecimal representation is commonly used in computer science and programming, especially when dealing with low-level operations or data encoding. In Python, converting a hex string to an integer is a frequent operation, and developers have multiple approaches at their disposal to achieve this tas
2 min read
Convert string to a list in Python Our task is to Convert string to a list in Python. Whether we need to break a string into characters or words, there are multiple efficient methods to achieve this. In this article, we'll explore these conversion techniques with simple examples. The most common way to convert a string into a list is
2 min read
Check If String is Integer in Python In this article, we will explore different possible ways through which we can check if a string is an integer or not. We will explore different methods and see how each method works with a clear understanding.Example:Input2 : "geeksforgeeks"Output2 : geeksforgeeks is not an IntigerExplanation : "gee
4 min read
Convert integer to string in Python In this article, weâll explore different methods for converting an integer to a string in Python. The most straightforward approach is using the str() function.Using str() Functionstr() function is the simplest and most commonly used method to convert an integer to a string.Pythonn = 42 s = str(n) p
2 min read
Convert String to Int in Python In Python, converting a string to an integer is important for performing mathematical operations, processing user input and efficiently handling data. This article will explore different ways to perform this conversion, including error handling and other method to validate input string during conver
3 min read
Converting all Strings in a List to Integers - Python We are given a list of strings containing numbers and our task is to convert these strings into integers. For example, if the input list is ["1", "2", "3"] the output should be [1, 2, 3]. Note: If our list contains elements that cannot be converted into integers such as alphabetic characters, string
2 min read
Convert Hex to String in Python Hexadecimal (base-16) is a compact way of representing binary data using digits 0-9 and letters A-F. It's commonly used in encoding, networking, cryptography and low-level programming. In Python, converting hex to string is straightforward and useful for processing encoded data.Using List Comprehens
2 min read
Convert String to Long in Python Python defines type conversion functions to directly convert one data type to another. This article is aimed at providing information about converting a string to long. Converting String to long A long is an integer type value that has unlimited length. By converting a string into long we are transl
1 min read
Python - Converting list string to dictionary Converting a list string to a dictionary in Python involves mapping elements from the list to key-value pairs. A common approach is pairing consecutive elements, where one element becomes the key and the next becomes the value. This results in a dictionary where each pair is represented as a key-val
3 min read
How to convert string to integer in Python? In Python, a string can be converted into an integer using the following methods : Method 1: Using built-in int() function: If your string contains a decimal integer and you wish to convert it into an int, in that case, pass your string to int() function and it will convert your string into an equiv
3 min read