Encode and Decode : Charset « I18N « Java






Encode and Decode

Encode and Decode
    
// : c12:EncodeDecode.java
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.

import java.nio.ByteBuffer;
import java.nio.charset.Charset;

public class EncodeDecode {

  public static void print(ByteBuffer bb) {
    while (bb.hasRemaining())
      System.out.print(bb.get() + " ");
    System.out.println();
    bb.rewind();
  }

  public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0,
        (byte) 'a' });
    System.out.println("Initial Byte Buffer");
    print(bb);
    Charset[] csets = { Charset.forName("UTF-16LE"),
        Charset.forName("UTF-16BE"), Charset.forName("UTF-8"),
        Charset.forName("US-ASCII"), Charset.forName("ISO-8859-1") };
    for (int i = 0; i < csets.length; i++) {
      System.out.println(csets[i].name() + ":");
      print(csets[i].encode(bb.asCharBuffer()));
      csets[i].decode(bb);
      bb.rewind();
    }

  }
} ///:~



           
         
    
    
    
  








Related examples in the same category

1.List the Charset in your system
2.Converting Between Strings (Unicode) and Other Character Set Encodings
3.Charset encoding test
4.encoder and decoder use a supplied ByteBuffer
5.Translate Charset
6.Detect non-ASCII characters in string
7.Displays Charsets and aliasesDisplays Charsets and aliases
8.extends CharsetDecoder to create Base64 Decoder
9.extends Charset to create Hex Charset
10.Get the default charset
11.Charset Toolkit
12.Defines common charsets supported in all Java platforms.
13.How to auto-detect a file's encoding