Parses a string (Chinese Character Unicode encoding) into Chinese characters and unicode encoding

Source: Internet
Author: User

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 ();}}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.