1

For example, I want 68656c6c6f, the hex encoding of the word 'hello', to be converted into [104, 101, 108, 108, 111]. I need it as a list, not as a continuous integer.

x=int("68656c6c6f",16) is apparently not what I'm looking for because it gives me 448378203247 instead, which makes sense. But it's not my solution.

Aswin G
  • 97
  • 2
  • 8
  • The question "hexadecimal string to byte array in python" (For which this question got marked as duplicate) does not give the answer I want. – Aswin G May 12 '16 at 19:42

2 Answers2

4

Use int(s, base=16),

s = "68656c6c6f"
x = [int(s[i:i+2], 16) for i in range(0, len(s), 2)]

print(x)
# Output
[104, 101, 108, 108, 111]

Or use ord as mentioned by @Liam

s = "68656c6c6f"
x = [ord(c) for c in s.decode('hex')]

print(x)
# Output
[104, 101, 108, 108, 111]
SparkAndShine
  • 17,001
  • 22
  • 90
  • 134
2

just do this:

hex_string = "68656c6c6f"

c_list = []

for c in hex_string.decode("hex"):
    c_list.append(ord(c))
Liam
  • 6,009
  • 4
  • 39
  • 53