Jump to content

UTF-8 encode and decode: Difference between revisions

Add python3 solution
(→‎{{header|Go}}: add implementation)
(Add python3 solution)
Line 469:
#1D11E -> {#F0,#9D,#84,#9E} -> {#1D11E}
</pre>
 
=={{header|Python}}==
<lang python>
#!/usr/bin/env python3
import unicodedata
 
 
def name(ch):
return unicodedata.name(ch)
 
 
def unicode_code(ch):
return 'U+{:04x}'.format(ord(ch))
 
 
def utf8hex(ch):
return " ".join([hex(c)[2:] for c in ch.encode('utf8')]).upper()
 
 
if __name__ == "__main__":
print('{:<11} {:<36} {:<15} {:<15}'.format('Character', 'Name', 'Unicode', 'UTF-8 encoding (hex)'))
chars = ['A', 'ö', 'Ж', '€', '𝄞']
for char in chars:
print('{:<11} {:<36} {:<15} {:<15}'.format(char, name(char), unicode_code(char), utf8hex(char)))</lang>
{{out}}
<pre>Character Name Unicode UTF-8 encoding (hex)
A LATIN CAPITAL LETTER A U+0041 41
ö LATIN SMALL LETTER O WITH DIAERESIS U+00f6 C3 B6
Ж CYRILLIC CAPITAL LETTER ZHE U+0416 D0 96
€ EURO SIGN U+20ac E2 82 AC
𝄞 MUSICAL SYMBOL G CLEF U+1d11e F0 9D 84 9E</pre>
 
=={{header|Racket}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.