Thursday, March 09, 2006

Some code to illustrate the unicode support by Character class

public class UnicodeTest{
public static void main(String... args) throws IOException
{
System.out.println("is valid codepoint : " + Character.isValidCodePoint(0x10FFFF));
System.out.println("is valid codepoint : " + Character.isValidCodePoint(0x20FFFF));
int cp = 0x10177;
System.out.println("is valid codepoint : " + Character.isValidCodePoint(cp));
char[] ch = new char[2];
ch = Character.toChars(cp);
int low = ch[0];
int high = ch[1];
System.out.println("Low Surrogate Pair : " + low + " Hexadecimal : " + Integer.toHexString(low) + " Binary String : " + Integer.toBinaryString(low));
System.out.println("High Surrogate Pair : " + high + " Hexadecimal : " + Integer.toHexString(high) + " Binary String : " + Integer.toBinaryString(high));
String st = new String(ch);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("krishna.xml"), "UTF-8"));
out.write("");
out.write("");
out.write(st);
out.write("
");
out.close();

}
}

In the above case, the supplementary character written in xml was not the one it is intended to be. Don't know whether it is the limitation of browser to display the supplementary characters or anything wrong in the way a supplementary character be handled in java code.

No comments: