C # code Analysis of converting Unicode encoding to Chinese character strings

Source: Internet
Author: User
Here's a small piece to bring you a C # to convert Unicode encoding into a Chinese character string simple method. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.

C # will JS in the Unicode conversion to a string, the Internet is not looking for, encountered a number of the turn, slightly changed the point, OK!

Examples are as follows:

///converts Unicode encoding to Chinese character string///////Unicode encoded string//Chinese character string public static s Tring ToGB2312 (String str) {MatchCollection MC = regex.matches (str, "([\\w]+) | (      \\\\u ([\\w]{4})); if (MC! = NULL && MC.        Count > 0) {StringBuilder sb = new StringBuilder (); foreach (Match m2 in MC) {string v = m2.          Value;            if (v.startswith ("\ \")) {string word = v.substring (2);            Byte[] codes = new byte[2]; int code = CONVERT.TOINT32 (Word.            Substring (0, 2), 16); int code2 = Convert.ToInt32 (Word.            Substring (2), 16);            Codes[0] = (byte) code2;            CODES[1] = (byte) code; Sb.          Append (Encoding.Unicode.GetString (codes)); } else {sb.          Append (v); }} return sb.      ToString ();      } else {return str; }    }
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.