Demonstrates how characters are translated to byte sequences by various Charset implementations. - Java Internationalization

Java examples for Internationalization:Charset

Introduction

Encoding with the standard charsets

Demo Code

 
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
public class Main {
  public static void main(String[] argv) throws Exception {
    String input = "\u00bfMa\u00f1ana?";
    String[] charsetNames = { "US-ASCII", "ISO-8859-1", "UTF-8", "UTF-16BE",
        "UTF-16LE", "UTF-16"
    };/*from  w w w  .  j  a  v  a 2s  . com*/
    for (int i = 0; i < charsetNames.length; i++) {
      doEncode(Charset.forName(charsetNames[i]), input);
    }
  }
  private static void doEncode(Charset cs, String input) {
    ByteBuffer bb = cs.encode(input);
    System.out.println("Charset: " + cs.name());
    System.out.println("  Input: " + input);
    System.out.println("Encoded: ");

    for (int i = 0; bb.hasRemaining(); i++) {
      int b = bb.get();
      int ival = ((int) b) & 0xff;
      char c = (char) ival;

      System.out.print("  " + i + ": ");
      if (ival < 16)
        System.out.print("0");
      System.out.print(Integer.toHexString(ival));
      if (Character.isWhitespace(c) || Character.isISOControl(c)) {
        System.out.println("");
      } else {
        System.out.println(" (" + c + ")");
      }
    }
    System.out.println("");
  }
}

Related Tutorials