0

I have a list of unicode characters that I want to write to a file. However, I only have them as their raw 16-bit hexcode.

How can I output proper unicode characters from a hexcode, i.e. how to I get from

00DF

to

ß

?

Note that ß is \u00DF in unicode.

Neuneck
  • 294
  • 1
  • 3
  • 14
  • 1
    Possible duplicate of [Convert unicode codepoint to UTF8 hex in python](https://stackoverflow.com/questions/867866/convert-unicode-codepoint-to-utf8-hex-in-python) – mvp Feb 18 '19 at 16:54
  • `s= '\u00DF';s.__repr__()`? or `s.__str__()`? – gabhijit Feb 18 '19 at 16:56
  • Why did you tag the post with `utf-8`? There's nothing about it in the question. – lenz Feb 18 '19 at 20:10
  • 1
    The linked question doesn't exactly fit, but you might be able to get enough hints from the comments over there. I guess you're looking for `chr(int('00DF', 16))`, but the question isn't really clear enough to me. – lenz Feb 18 '19 at 20:13
  • @lenz Your suggestion works. If it were an answer, I could accept it. – Neuneck Feb 19 '19 at 13:18
  • @lenz: Note: For surrogates (so unicode code point > 16 bits) the example will not give the correct result, but the example is the correct way to go – Giacomo Catenazzi Feb 19 '19 at 13:23
  • 2
    Possible duplicate of [Convert from hexa to Arabic text with Python](https://stackoverflow.com/questions/40927544/convert-from-hexa-to-arabic-text-with-python) – tripleee Feb 19 '19 at 13:41
  • Another possible duplicate: https://stackoverflow.com/questions/42463940/how-to-convert-hexadecimal-string-to-character-with-that-code-point – tripleee Feb 19 '19 at 13:42
  • @GiacomoCatenazzi Very good point about the surrogates. [Martin Pieters' answer linked above](https://stackoverflow.com/a/40927836/) is the way to go I'd say, ie. `binascii.unhexlify` with `decode('utf-16-be')`. – lenz Feb 20 '19 at 13:46

0 Answers0