Reputation: 601
I need to XOR through a list of hex values but they are extracted from a text file so they are as strings how can I turn them into hex values?
An example list:
['02', '0E', '00', '12', 'D2', '00', '00', '00', '00', '00', '00',
'00', '00', '00', '00', 'CC', '02', '0C', '00', '10', '03', '00',
'00', '00', '00', '00', '00', '00', '00', '1D', '02', '0A', '00',
'04', '7E', 'F3', '34', '00', '00', '00', '00', 'B5', '02', '0E',
'00', '12', 'CF', '00', '00', '00', '00', '00', '00', '00', '00',
'00', '00', 'D1', '02', '0E', '00', '12', 'CC', '00', '00', '00',
'00', '00', '00', '00', '00', '00', '00', 'D2', '02', '0A', '00',
'04', '7F', 'F3', '34', '00', '00', '00', '00', 'B4', '02', '0A',
'02', '0E', '00', '12', 'CF', '00', '00', '00', '00', '00']
Upvotes: 2
Views: 133
Reputation: 399753
Use the built-in function int()
:
>> print(int('0e', 16))
14
The second argument tells int()
to expect numbers in hex, i.e. base 16.
Note that there is no such thing as a "hex value"; hexadecimal is just a notation used when printing the number. 14
and 0xe
are the same number.
You can of course convert the entire list using a list comprehension:
list2 = [int(x, 16) for x in list1]
Assuming the original list of strings is list1
.
If you want to print the numbers in hex, use hex()
, another built-in:
>>> print(hex(int('e', 16)))
0xe
Upvotes: 3