Search code examples
javaxmlunicodeunicode-escapes

Get Character Representation of a Unicode value In Java


I want the character representation of a Unicode value in Java. Can this be done ?

Some characters (example is the character whose unicode value is \u001b) are not supported in XML. So I am escaping them in the XML by putting the Unicode value '\u001b' and after unmarshalling, I want the character representation of \u001b to displayed. Can this be done in Java ?

Suggestions are welcome.


Solution

  • try this

        String s = "\\u0031";
        char c = (char)Integer.parseInt(s.substring(2), 16);
        System.out.print(c);
    

    output

    1
    

    though I would suggest to use XML numeric character references http://en.wikipedia.org/wiki/Numeric_character_reference like  then it would be decoded by XML parser automatically