Advertisement
hhac

Standard Charsets Example

May 26th, 2021 (edited)
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.76 KB | None | 0 0
  1.  
  2. import java.nio.charset.Charset;
  3. import java.nio.charset.StandardCharsets;
  4.  
  5. public class CharsetExample {
  6.  
  7.     /**
  8.      * US-ASCII Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set
  9.      * ISO-8859-1   ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1
  10.      * UTF-8    Eight-bit UCS Transformation Format
  11.      * UTF-16BE Sixteen-bit UCS Transformation Format, big-endian byte order
  12.      * UTF-16LE Sixteen-bit UCS Transformation Format, little-endian byte order
  13.      * UTF- 16  Sixteen-bit UCS Transformation Format, byte order identified by an optional byte-order mark
  14.      */
  15.     public static void main(String[] args) {
  16.         String str = "HECTORÑñªçºಚÉ";
  17.  
  18.         printBytesValues(str);
  19.         printBytesValues(str, StandardCharsets.US_ASCII);
  20.         printBytesValues(str, StandardCharsets.UTF_8);
  21.         printBytesValues(str, StandardCharsets.ISO_8859_1);
  22.         printBytesValues(str, StandardCharsets.UTF_16);
  23.         printBytesValues(str, StandardCharsets.UTF_16BE);
  24.         printBytesValues(str, StandardCharsets.UTF_16LE);
  25.     }
  26.  
  27.     static void printBytesValues(String value) {
  28.  
  29.         byte[] data = value.getBytes();
  30.  
  31.         System.out.print("Value: " + value + ", Data: ");
  32.         for (int i = 0; i < data.length; i ++) {
  33.             System.out.print(String.format("[%d]", data[i]));
  34.         }
  35.         System.out.print("\n");
  36.     }
  37.  
  38.     static void printBytesValues(String value, Charset charset) {
  39.  
  40.         byte[] data = value.getBytes(charset);
  41.  
  42.         System.out.print("Value: " + value + ", Charset: " + charset + ", Data: ");
  43.         for (int i = 0; i < data.length; i ++) {
  44.             System.out.print(String.format("[%d]", data[i]));
  45.         }
  46.         System.out.print("\n");
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement