Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.nio.charset.Charset;
- import java.nio.charset.StandardCharsets;
- public class CharsetExample {
- /**
- * US-ASCII Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set
- * ISO-8859-1 ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1
- * UTF-8 Eight-bit UCS Transformation Format
- * UTF-16BE Sixteen-bit UCS Transformation Format, big-endian byte order
- * UTF-16LE Sixteen-bit UCS Transformation Format, little-endian byte order
- * UTF- 16 Sixteen-bit UCS Transformation Format, byte order identified by an optional byte-order mark
- */
- public static void main(String[] args) {
- String str = "HECTORÑñªçºಚÉ";
- printBytesValues(str);
- printBytesValues(str, StandardCharsets.US_ASCII);
- printBytesValues(str, StandardCharsets.UTF_8);
- printBytesValues(str, StandardCharsets.ISO_8859_1);
- printBytesValues(str, StandardCharsets.UTF_16);
- printBytesValues(str, StandardCharsets.UTF_16BE);
- printBytesValues(str, StandardCharsets.UTF_16LE);
- }
- static void printBytesValues(String value) {
- byte[] data = value.getBytes();
- System.out.print("Value: " + value + ", Data: ");
- for (int i = 0; i < data.length; i ++) {
- System.out.print(String.format("[%d]", data[i]));
- }
- System.out.print("\n");
- }
- static void printBytesValues(String value, Charset charset) {
- byte[] data = value.getBytes(charset);
- System.out.print("Value: " + value + ", Charset: " + charset + ", Data: ");
- for (int i = 0; i < data.length; i ++) {
- System.out.print(String.format("[%d]", data[i]));
- }
- System.out.print("\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement