Anonymous user
UTF-8 encode and decode: Difference between revisions
→{{header|Java}}: remove duplicate code, remove antique exception
(→{{header|Python}}: Uses "name" directly.) |
(→{{header|Java}}: remove duplicate code, remove antique exception) |
||
Line 344:
=={{header|Java}}==
{{works with|Java|7+}}
<lang java>import java.
import java.
public class UTF8EncodeDecode {
public static byte[]
return new String(new int[]{codepoint}, 0, 1).getBytes(StandardCharsets.UTF_8);
}
public static int utf8decode(byte[] bytes)
return new String(bytes,
}
public static
System.out.printf("%-7s %-43s %7s\t%s\t%7s\n", "Char", "Name", "Unicode", "UTF-8 encoded", "Decoded");▼
System.out.printf("%-7s %-43s %7s\t%s\t%7s%n",
▲
System.out.printf("%-7c %-43s U+%04X\t%-12s\tU+%04X\n", codepoint, Character.getName(codepoint), codepoint, encodedHex, decoded);▼
▲ System.out.printf("%-7s %-43s %7s\t%s\t%s\n", "Char", "Name", "Unicode", "UTF-8 encoded", "Decoded");
for (int codepoint : new int[]{0x0041, 0x00F6, 0x0416, 0x20AC, 0x1D11E}) {
byte[] encoded = utf8encode(codepoint);
Line 377 ⟶ 369:
String encodedHex = formatter.toString();
int decoded = utf8decode(encoded);
System.out.printf("%-7c %-43s U+%04X\t%-12s\
▲
}
}
|