0

I trying to decode a hex string in python.

value = ""
for i in "54 C3 BC 72 20 6F 66 66 65 6E 20 4B 6C 69 6D 61".split(" "):
  value += chr(int(i, 16))
print(value)

Result:

Tür offen Klima

Expected result should be "Tür offen Klima" How can i make this work properly ?

3
  • You are "decoding" a unicode string, but your data is not unicode code point, but bytes of UTF-8. So: try with binarray (in UTF-8) and convert to unicode later Commented Aug 10, 2020 at 7:53
  • 1
    "Tür offen Klima" is encoded to "54FC72206F6666656E204B6C696D61". Please check your hex string, it simply looks wrong Commented Aug 10, 2020 at 7:53
  • @giacomo-catenazzi can you please explain it a litle more ? your commet is a litle bit to short for me to full understand the problem. Commented Aug 10, 2020 at 8:02

2 Answers 2

2

Your data is encoded as UTF-8, which means that you sometimes have to look at more than one byte to get one character. The easiest way to do this is probably to decode your string into a sequence of bytes, and then decode those bytes into a string. Python has built-in features for both:

value = bytes.fromhex("54 C3 BC").decode("utf-8")

Sign up to request clarification or add additional context in comments.

Comments

0

The problem is the result of the string

"54 C3 BC 72 20 6F 66 66 65 6E 20 4B 6C 69 6D 61"

is indeed

Tür offen Klima

The proper hex string that result in "Tür offen Klima" is actually:

"54 FC 72 20 6F 66 66 65 6E 20 4B 6C 69 6D 61"

Therefore, the code below would generate the result you expected:

value = ""
for i in "54 FC 72 20 6F 66 66 65 6E 20 4B 6C 69 6D 61".split(" "):
    value += chr(int(i, 16))
print(value)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.