parsecer parsecer -4 years ago 134
Java Question

Java display unicode code points instead of string literal

Suppose I have a

String s = "green"
. What should I do to get its Unicode representation
"\u0067\u0072\u0065\u0065\u006E"
?

Answer Source

I don't think that there is any method which will provide this functionality, however if you were to build a custom method, then you can do something like this:

private static String toUnicodeString(String str) {
    String ret = "";
    for (char ch: str.toCharArray())
        ret += String.format("\\u%04x", (int) ch);
    return ret;
}

and so toUnicodeString("green") returns \u0067\u0072\u0065\u0065\u006e

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download