0

I tried to decode a hex string but special characters.

When I run

codecs.decode("5469eb73746f2026204b53484d5220666561742e205661737379", "hex")

I get b'Ti\xebsto & KSHMR feat. Vassy'

but I want Tiësto & KSHMR feat. Vassy

I checked the Hex code online but it is correct. Do I need another function or do I just miss one step?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
cre8
  • 13,012
  • 8
  • 37
  • 61
  • I used `print(codecs.decode("5469eb73746f2026204b53484d5220666561742e205661737379", "hex"))` and get `b'Ti\xebsto & KSHMR feat. Vassy'` – cre8 Sep 10 '15 at 17:38
  • You have a byte value, yes, so decode it to Unicode first. – Martijn Pieters Sep 10 '15 at 17:41
  • 1
    Please, in future, include the *full* output you got; the `b'..'` prefix there told me that you are using Python 3, not Python 2 as I initially had to assume (statistically speaking Python 2 would have been more likely). – Martijn Pieters Sep 10 '15 at 17:44

1 Answers1

4

You decoded the hex value to a bytes object. If you expected (Unicode) text, decode the bytes with a valid encoding; you appear to have either Latin 1 or Windows Codepage 1252 data here:

>>> import codecs
>>> codecs.decode("5469eb73746f2026204b53484d5220666561742e205661737379", "hex")
b'Ti\xebsto & KSHMR feat. Vassy'
>>> _.decode('latin1')
'Tiësto & KSHMR feat. Vassy'
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • @JoranBeasley: `str.encode()` and `bytes.decode()` are entirely consistent in Python 3. You need to use `codecs.encode()` / `codecs.decode()` to get to the [transforming codecs](https://docs.python.org/3/library/codecs.html#python-specific-encodings) such as `'hex'`. – Martijn Pieters Sep 10 '15 at 17:48