Charset encoding test : Charset « I18N « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » I18N » Charset 




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

public class MainClass {
  public static void main(String[] argvthrows Exception {
    String input = "\u00bfMa\u00f1ana?";

    // The list of charsets to encode with
    String[] charsetNames = "US-ASCII""ISO-8859-1""UTF-8""UTF-16BE""UTF-16LE""UTF-16",
    // "X-ROT13" // This requires META-INF/services
    };
    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 = ((intb0xff;
      char c = (charival;
      // print index number
      System.out.print("  " + i + ": ");
      // print the hex value of the byte
      System.out.print(Integer.toHexString(ival));
      System.out.println(" (" + c + ")");
    }
  }
}

           
         
    
    
    
  














Related examples in the same category
1.List the Charset in your system
2.Converting Between Strings (Unicode) and Other Character Set Encodings
3.encoder and decoder use a supplied ByteBuffer
4.Translate Charset
5.Detect non-ASCII characters in string
6.Displays Charsets and aliasesDisplays Charsets and aliases
7.Encode and DecodeEncode and Decode
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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.