Create a StringReader for the string containing unicode escape sequences and then use the read() method to read it in. I think that will convert them to their real characters. If not try this code:
Code:/** * Convert a string so any unicode escaped character sequences are converted * back to their UTF-16 character codes * * @param text - the text to convert * @return the converted text */ public static String fromUnicode(String text) { if ( text == null ) return null; char[] in = text.toCharArray(); char[] out = new char[in.length]; char aChar; int outLen = 0; int off = 0; int end = in.length; while ( off < end ) { aChar = in[off++]; if ( aChar == '\\' ) { // handle escaped characters aChar = in[off++]; if ( aChar == 'u' ) { // handle unicode // Read the xxxx int value = 0; for ( int i = 0; i < 4; i++ ) { aChar = in[off++]; switch ( aChar ) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': value = (value << 4) + aChar - '0'; break; case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': value = (value << 4) + 10 + aChar - 'a'; break; case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': value = (value << 4) + 10 + aChar - 'A'; break; default: throw new IllegalArgumentException( "Malformed \\uxxxx encoding."); } } out[outLen++] = (char)value; } else { // handle other escaped chars if ( aChar == 't' ) aChar = '\t'; else if ( aChar == 'r' ) aChar = '\r'; else if ( aChar == 'n' ) aChar = '\n'; else if ( aChar == 'f' ) aChar = '\f'; else if ( aChar == '\\' ) aChar = '\\'; else if ( aChar == 'b' ) aChar = '\b'; else if ( aChar == '"' ) aChar = '"'; out[outLen++] = aChar; } } else { // handle non escaped characters out[outLen++] = aChar; } } return new String(out, 0, outLen); }




Reply With Quote