Unicode to string conversion in Java


To convert the given Unicode to an character:
i.e String unicode = “0”+91+D1+””; to a Unicode character

Following method can use in Java

char getUnicode(String codePoint) {
   return (char)  Integer.parseInt(codePoint, 16);
}
or 

String string = "0061";
char c = (char) Integer.parseInt(string, 16);
System.out.println(c);
Advertisements

Reading unicode from plain text/file


Reading Unicode characters from a raw text and want to use as Unicode character, require to unescape the character first and then can be used as Unicode.

The code below can be used for the same.

String unescape(String s) {
    int i=0,len=s.length();
    char c;
    StringBuffer sb = new StringBuffer(len);
    while (i<len) {
        c = s.charAt(i++);
        if (c=='\\') {
            if (i<len) {
               c = s.charAt(i++);
               if (c=='u'  || c == 'U' ) {
                   c = (char) Integer.parseInt(s.substring(i,i+4),16);
                   i += 4;
               } // add other cases here as desired...
           }
        } // fall through: \ escapes itself, quotes any character but u
       sb.append(c);
    }
return sb.toString();
}

Blog at WordPress.com.

%d bloggers like this: