Parses a string (Chinese Character Unicode encoding) into Chinese characters and unicode encoding
Prerequisites: the server uses a. Net website, while the android client is developed in Java. The data transmission format used again is in Json format.
Generally, projects are developed using the java language on the server. Therefore, although Json format is used for data transmission, Unicode encoding is not performed in the data, in this project, since it was developed using the C # language and the transmitted data contains the Unicode encoding of Chinese characters, the Unicode encoding needs to be decoded on the client, I found the method provided by the string and did not find a method to directly decode the Chinese character. So I searched for the information and wrote a tool class for decoding, if you know other decoding methods, please leave a message and learn from each other! See the specific source code below:
Package com. momo. exchangeserver;/***** @ Description: parses a string containing Unicode Chinese characters * @ author: * @ see: * @ since: * @ copyright©* @ Date: */public class DecodeUnicode {/*** parses Unicode into Chinese characters * @ param pStr (Unicode of Chinese characters) encoded String * @ return */public String decodeUnicode (String pStr) {char aChar; int len = pStr. length (); StringBuffer outBuffer = new StringBuffer (len); for (int x = 0; x <len;) {aChar = pStr. charAt (x ++); if (aChar = '\') {aChar = pStr. charAt (x ++); if (aChar = 'U') {int value = 0; for (int I = 0; I <4; I ++) {aChar = pStr. charAt (x ++); 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 encoding. ") ;}} outBuffer. append (char) value);} else {if (aChar = 'T') {aChar = '\ T';} else if (aChar = 'R ') {aChar = '\ R';} else if (aChar = 'n') {aChar =' \ n';} else if (aChar = 'F ') {aChar = '\ F';} outBuffer. append (aChar) ;}} else {outBuffer. append (aChar) ;}} return outBuffer. toString ();}}