How would I convert characters which are surrogate pairs into hexadecimal?
I've found that using hex()
and ord()
works for characters with a single code point, such as emojis like "". For example:
print(hex(ord("")))
# '0x1f600'
Similarly, using chr()
and int()
works for getting the characters from the hexadecimal:
print(chr(int(0x1f600)))
# ''
However, as soon as I use a surrogate pair, such as an emoji like "", the code throws an error:
print(hex(ord("")))
TypeError: ord() expected a character, but string of length 2 found
How would I fix this, and how would I convert such hexadecimal back into a character?
Since an exact output format wasn't specified, how about:
Output:
Or similar to your original code:
Output: