The hex() function converts a specified integer number into a hexadecimal string representation.
Use int(x, base) with 16 as base to convert the string x to an integer. Call hex(number) with the integer as number to convert it to hexadecimal.
hex_string = "0xAA"
"0x" also required
an_integer = int(hex_string, 16)
hex_value = hex(an_integer)
print(hex_value)
Output
Answer from Thuấn Đào Minh on Stack Overflow0xaa
I am wanting to take a normal string, "My water bottle is blue" for example, and converting that into the HEX representation of how it would be stored in a txt file. I've tried .encode, binascii.hexlify, and a few other things. Any help would be appreciated.
python - How to convert hex str into int array - Stack Overflow
How to convert character to hexadecimal and back?
How to convert a string to array and convert each of the hex value in array to decimal in python? - Stack Overflow
arrays - How to convert hexadecimal string to bytes in Python? - Stack Overflow
You could range(..) over substrings of length 2:
c = '8db6796fee32785e366f710df10cc'
c2=[int(c[i:i+2],16) for i in range(0,len(c),2)]
So i iterates of the string with steps of 2 and you take a substring of length 2 from i to i+2 (exclusive) with c[i:i+2]. These you convert by taking int(..,16).
For your sample input it generates:
>>> c='8db6796fee32785e366f710df10cc'
>>> [int(c[i:i+2],16) for i in range(0,len(c),2)]
[141, 182, 121, 111, 238, 50, 120, 94, 54, 111, 113, 13, 241, 12, 12]
The last element is 12 because the length of your string is odd, so it takes c as the last element to parse.
For an alternate approach
hex_string = '8db6796fee32785e366f710df10cc542B4'
a = bytearray.fromhex(hex_string)
b = list(a)
print(b)
If you're using Python 3.5+ you can use the bytes.fromhex method to convert hex string to bytes, and use the list constructor to convert bytes into a list of integers:
>>> list(bytes.fromhex('7f33117cf266a525'))
[127, 51, 17, 124, 242, 102, 165, 37]
And you can use the bytes constructor to convert a list of integers to bytes, and use the bytes.hex method to convert bytes to hex string:
>>> bytes([127, 51, 17, 124, 242, 102, 165, 37]).hex()
'7f33117cf266a525'
Try this to separate every two characters in the string :
T = [string[i:i+2] for i in range(0, len(string), 2)]
# T = ['7f', '33', '11', '7c', 'f2', '66', 'a5', '25']
However, if you have odd number of characters in string and want to get a list of every two characters starting from first, then try this :
T = list(map(''.join, zip(*[iter(string)]*2)))
# T = ['7f', '33', '11', '7c', 'f2', '66', 'a5', '25']
Difference is, if string = '7f33117cf266a5251', first list comprehension returns ['7f', '33', '11', '7c', 'f2', '66', 'a5', '25', '1'] whereas the second one still returns ['7f', '33', '11', '7c', 'f2', '66', 'a5', '25']
Suppose your hex string is something like
>>> hex_string = "deadbeef"
Convert it to a bytearray (Python 3 and 2.7):
>>> bytearray.fromhex(hex_string)
bytearray(b'\xde\xad\xbe\xef')
Convert it to a bytes object (Python 3):
>>> bytes.fromhex(hex_string)
b'\xde\xad\xbe\xef'
Note that bytes is an immutable version of bytearray.
Convert it to a string (Python ≤ 2.7):
>>> hex_data = hex_string.decode("hex")
>>> hex_data
"\xde\xad\xbe\xef"
There is a built-in function in bytearray that does what you intend.
bytearray.fromhex("de ad be ef 00")
It returns a bytearray and it reads hex strings with or without space separator.