11-25-2022, 11:54 AM
Easiest Way to Convert List of Hex Strings to List of Integers
5/5 – (1 vote)
Question: Given a Python list of hexadecimal strings such as . How to convert it to a list of integers in Python such as ?
Easiest Answer
![[Image: image-266-1024x559.png]](https://blog.finxter.com/wp-content/uploads/2022/11/image-266-1024x559.png)
The easiest way to convert a list of hex strings to a list of integers in Python is the list comprehension statement that applies the built-in function to convert each hex string to an integer using the hexadecimal base , and repeats this for each hex string in the original list.
Here’s a minimal example:
The list comprehension statement applies the expression to each element in the list and puts the result of this expression in the newly-created list.
The expression converts a hex string to an integer using the hexadecimal base argument . A semantically identical way to write this would be .
In fact, there are many more ways to convert a hex string to an integer—each of them could be used in the expression part of the list comprehension statement.
However, I’ll show you one completely different approach to solving this problem without listing each and every combination of possible solutions.![[Image: 1f447.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png)
For Loop with List Append
You can create an empty list and add one hex integer at a time in the loop body after converting it from the hex string using the function call. This first creates a hexadecimal string with prefix using string concatenation and then lets Python evaluate the string as if it was real code and not a string.
Here’s an example:
You use the fact that Python automatically converts a hex value of the form to an integer :
You may want to check out my in-depth guide on this important function for our solution:
Recommended Tutorial: Python
5/5 – (1 vote)
Question: Given a Python list of hexadecimal strings such as Code:
['ff', 'ef', '0f', '0a', '93']Code:
[255, 239, 15, 10, 147]Easiest Answer
![[Image: image-266-1024x559.png]](https://blog.finxter.com/wp-content/uploads/2022/11/image-266-1024x559.png)
The easiest way to convert a list of hex strings to a list of integers in Python is the list comprehension statement
Code:
[int(x, 16) for x in my_list]Code:
int()Code:
16Code:
xHere’s a minimal example:
Code:
my_list = ['ff', 'ef', '0f', '0a', '93']
my_ints = [int(x, 16) for x in my_list] print(my_ints)
# [255, 239, 15, 10, 147]The list comprehension statement applies the expression
Code:
int(x, 16)Code:
xCode:
my_listThe
Code:
int(x, 16)Code:
16Code:
int(x, base=16)In fact, there are many more ways to convert a hex string to an integer—each of them could be used in the expression part of the list comprehension statement.
However, I’ll show you one completely different approach to solving this problem without listing each and every combination of possible solutions.
![[Image: 1f447.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png)
For Loop with List Append
You can create an empty list and add one hex integer at a time in the loop body after converting it from the hex string
Code:
xCode:
eval('0x' + x)Code:
'0x'Here’s an example:
Code:
my_list = ['ff', 'ef', '0f', '0a', '93'] my_ints = []
for x in my_list: my_ints.append(eval('0x' + x)) print(my_ints)
# [255, 239, 15, 10, 147]You use the fact that Python automatically converts a hex value of the form
Code:
0xffCode:
255Code:
>>> 0xff
255
>>> 0xfe
254
>>> 0x0f
15You may want to check out my in-depth guide on this important function for our solution:
Recommended Tutorial: Python Code:
eval()
