This is the kind of simple code Jon Skeet had in mind in his comment:
final String in = "????asdf";
final StringBuilder out = new StringBuilder();
for (int i = 0; i < in.length(); i++) {
final char ch = in.charAt(i);
if (ch <= 127) out.append(ch);
else out.append("\u").append(String.format("%04x", (int)ch));
}
System.out.println(out.toString());
As Jon said, surrogate pairs will be represented as a pair of u
escapes.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…