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.

1
  • The question "hexadecimal string to byte array in python" (For which this question got marked as duplicate) does not give the answer I want. Commented May 12, 2016 at 19:42

2 Answers 2

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]
Sign up to request clarification or add additional context in comments.

Comments

2

just do this:

hex_string = "68656c6c6f"

c_list = []

for c in hex_string.decode("hex"):
    c_list.append(ord(c))

1 Comment

Your soluation is more elegant and straightforward. It should be accepted.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.