Let us use the character Latin Capital Letter a with Ogonek (U+0104) as an example.
I have an int that represents its UTF-8 encoded form:
my_int = 0xC484
# Decimal: `50308`
# Binary: `0b1100010010000100`
If use the unichr function i get: \uC484 or 쒄 (U+C484)
But, I need it to output: Ą
How do I convert my_int to a Unicode code point?
To convert the integer 0xC484 to the bytestring '\xc4\x84' (the UTF-8 representation of the Unicode character Ą), you can use struct.pack():
>>> import struct
>>> struct.pack(">H", 0xC484)
'\xc4\x84'
... where > in the format string represents big-endian, and H represents unsigned short int.
Once you have your UTF-8 bytestring, you can decode it to Unicode as usual:
>>> struct.pack(">H", 0xC484).decode("utf8")
u'\u0104'
>>> print struct.pack(">H", 0xC484).decode("utf8")
Ą
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With