Question

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.

Was it helpful?

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

OTHER TIPS

 String fileName = "outputFile.txt";
 String str = "String with unicode" ;

try {
    FileOutputStream fos = new FileOutputStream(fileName);
    Writer out = new OutputStreamWriter(fos, "UTF8");
    out.write(str);
    out.close();
} catch (IOException e) {
    e.printStackTrace(System.err);
}

This should do

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top