. Net (c #) Chinese Character and Unicode encoding are mutually converted,

Source: Internet
Author: User

. Net (c #) Chinese Character and Unicode encoding are mutually converted,

{"Tilte": "\u535a\u5ba2\u56ed", "Href": "http://www.cnblogs.com"}

This json string is often used to convert the Chinese characters into Unicode codes.

Unicode encoding:

UNICODE encoding of Chinese characters, such as "King", which becomes "\ King". UNICODE characters start with \ u and are followed by four numbers or letters, all characters are hexadecimal numbers. Each two character represents a number less than 256. A Chinese character is composed of two characters, so it is easy to understand that "738b" is two characters, namely "73" and "8b ". However, when converting the UNICODE character encoding content to Chinese characters, the characters are processed forward from the back,You need to combine characters in the order of "8b" and "73" to obtain Chinese characters..

Unicode/Chinese character conversion implementation:
/// <Summary> // convert the string to Unicode // </summary> /// <param name = "source"> source string </ param> // <returns> Unicode encoded string </returns> public static string String2Unicode (string source) {byte [] bytes = Encoding. unicode. getBytes (source); StringBuilder stringBuilder = new StringBuilder (); for (int I = 0; I <bytes. length; I + = 2) {stringBuilder. appendFormat ("\ u {0} {1}", bytes [I + 1]. toString ("x "). padLeft (2, '0'), bytes [I]. toString ("x "). padLeft (2, '0');} return stringBuilder. toString ();} /// <summary> // convert Unicode to string /// </summary> /// <param name = "source"> Unicode-encoded string </param>/ // <returns> normal string </returns> public static string Unicode2String (string source) {return new Regex (@ "\ u ([0-9A-F] {4})", RegexOptions. ignoreCase | RegexOptions. compiled ). replace (source, x => string. empty + Convert. toChar (Convert. toUInt16 (x. result ("$1"), 16 )));}

 

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.