I started with it, but there is still a bug inside. The last sign consumes two chars.
Code is for Python 3.x
Don't post this code. What I whish: a good replacement for str_to_hex_str_with_space
And a bugfix for the last sign. It takes two spaces.
After it has been fixed, we should put get_table and print_table in one function.
It's also possible to use directly the unicode signs instead of codepoints.
Edit: I'm not able to post the last sign. The forum doesn't accept it :-(
Code is for Python 3.x
from binascii import hexlify import unicodedata def str_to_hex_str_with_space(char): hex_bytes = hexlify(char.encode()) hex_str = hex_bytes.decode() hex_str = ' '.join(hex_str[n:n+2] for n in range(0, len(hex_str), 2)) return hex_str.upper() def get_table(code_points): for code in code_points: char = chr(code) name = unicodedata.name(char) unic = 'U+{:05X}'.format(code) encoded = str_to_hex_str_with_space(char) yield char, name, unic, encoded, char def print_table(code_points): header = ['Char', 'Name', 'Unicode', 'UTF-8', 'Decoded'] fmt_str = '{:<10s}{:<45s}{:<10s}{:<10s}' print(fmt_str.format(*header)) for row in get_table(code_points): row = fmt_str.format(*row) print(row) if __name__ == '__main__': code_points = (0x0041, 0x00F6, 0x0416, 0x20AC, 0x1D11E) print_table(code_points)Output:
Output:Char Name Unicode UTF-8
A LATIN CAPITAL LETTER A U+00041 41
ö LATIN SMALL LETTER O WITH DIAERESIS U+000F6 C3 B6
Ж CYRILLIC CAPITAL LETTER ZHE U+00416 D0 96
€ EURO SIGN U+020AC E2 82 AC
? MUSICAL SYMBOL G CLEF U+1D11E F0 9D 84 9E
Someone other should improve it.Don't post this code. What I whish: a good replacement for str_to_hex_str_with_space
And a bugfix for the last sign. It takes two spaces.
After it has been fixed, we should put get_table and print_table in one function.
It's also possible to use directly the unicode signs instead of codepoints.
Edit: I'm not able to post the last sign. The forum doesn't accept it :-(
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!