Java tutorial
//package com.java2s; // License & terms of use: http://www.unicode.org/copyright.html#License public class Main { static final char DIGITS[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; /** * Escape unprintable characters using <backslash>uxxxx notation * for U+0000 to U+FFFF and <backslash>Uxxxxxxxx for U+10000 and * above. If the character is printable ASCII, then do nothing * and return FALSE. Otherwise, append the escaped notation and * return TRUE. */ public static boolean escapeUnprintable(StringBuffer result, int c) { if (isUnprintable(c)) { result.append('\\'); if ((c & ~0xFFFF) != 0) { result.append('U'); result.append(DIGITS[0xF & (c >> 28)]); result.append(DIGITS[0xF & (c >> 24)]); result.append(DIGITS[0xF & (c >> 20)]); result.append(DIGITS[0xF & (c >> 16)]); } else { result.append('u'); } result.append(DIGITS[0xF & (c >> 12)]); result.append(DIGITS[0xF & (c >> 8)]); result.append(DIGITS[0xF & (c >> 4)]); result.append(DIGITS[0xF & c]); return true; } return false; } /** * Return true if the character is NOT printable ASCII. The tab, * newline and linefeed characters are considered unprintable. */ public static boolean isUnprintable(int c) { return !(c >= 0x20 && c <= 0x7E); } }