I need to convert unicode string to string which have non-ascii characters encoded in unicode. For example, string "漢字 Max" should be presented as "\u6F22\u5B57 Max".
What I have tried:
Differenct combinations of
new String(sourceString.getBytes(encoding1), encoding2)
Apache StringEscapeUtils which escapes also ascii chars like double-quote
StringEscapeUtils.escapeJava(source)
Is there an easy way to encode such string? Ideally only Java 6 SE or Apache Commons should be used to achieve desired result.
\uescape sequences?)\uescapes, but handle aspects such as surrogates and ASCII escapes differently. If you are only generating user-readable text maybe you don't care and “any old format with\uin” is good enough, but if you're eg creating JSON, you'll need to use the exact rules for JSON escaping.