UTF-8 encode and decode: Difference between revisions

Line 616:
𝄞 U+1D11E F09D849E 𝄞
</pre>
 
=={{header|Groovy}}==
{{trans|Java}}
<lang groovy>import java.nio.charset.StandardCharsets
 
class UTF8EncodeDecode {
static byte[] utf8encode(int codePoint) {
char[] characters = [codePoint]
new String(characters, 0, 1).getBytes StandardCharsets.UTF_8
}
 
static int utf8decode(byte[] bytes) {
new String(bytes, StandardCharsets.UTF_8).codePointAt(0)
}
 
static void main(String[] args) {
printf "%-7s %-43s %7s\t%s\t%7s%n", "Char", "Name", "Unicode", "UTF-8 encoded", "Decoded"
 
([0x0041, 0x00F6, 0x0416, 0x20AC, 0x1D11E]).each { int codePoint ->
byte[] encoded = utf8encode codePoint
Formatter formatter = new Formatter()
encoded.each { byte b ->
formatter.format "%02X ", b
}
String encodedHex = formatter.toString()
int decoded = utf8decode encoded
printf "%-7c %-43s U+%04X\t%-12s\tU+%04X%n", codePoint, Character.getName(codePoint), codePoint, encodedHex, decoded
}
}
}</lang>
{{out}}
<pre>Char Name Unicode UTF-8 encoded Decoded
A LATIN CAPITAL LETTER A U+0041 41 U+0041
ö LATIN SMALL LETTER O WITH DIAERESIS U+00F6 C3 B6 U+00F6
Ж CYRILLIC CAPITAL LETTER ZHE U+0416 D0 96 U+0416
€ EURO SIGN U+20AC E2 82 AC U+20AC
𝄞 MUSICAL SYMBOL G CLEF U+1D11E ED 84 9E U+D11E</pre>
 
=={{header|Haskell}}==
1,452

edits