EncodeTestpublic class EncodeTest extends Object Charset encoding test. Run the same input string, which contains
some non-ascii characters, through several Charset encoders and dump out
the hex values of the resulting byte sequences. |
Methods Summary |
---|
private static void | doEncode(java.nio.charset.Charset cs, java.lang.String input)For a given Charset and input string, encode the chars
and print out the resulting byte encoding in a readable form.
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;
// keep tabular alignment pretty
if (i < 10) System.out.print (" ");
// print index number
System.out.print (" " + i + ": ");
// Better formatted output is coming for NIO
if (ival < 16) System.out.print ("0");
// print the hex value of the byte
System.out.print (Integer.toHexString (ival));
// If the byte seems to be the value of a
// printable character, print it. No guarantee
// it will be.
if (Character.isWhitespace (c) ||
Character.isISOControl (c))
{
System.out.println ("");
} else {
System.out.println (" (" + c + ")");
}
}
System.out.println ("");
| public static void | main(java.lang.String[] argv)
// This is the character sequence to encode
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);
}
|
|