C # Processing of Chinese in JSON-based communication

Source: Internet
Author: User

If the \\u4e00-\\u9fa5 range of Chinese Unicode code is generated in the communication, instead of the \u4e00-\u9fa5 range, then C # processing is more troublesome.

Crack method: Mechanism

It will split the \\u4e00 into parts to identify: \ \ u4e00, the magic is that in this case, it will think u4e000 is a whole.

Regular expressions

Regular expressions need to be written like this: "[\\\\4e00-\\\\u9fa5]", it WORKS!!! Although it is a two-part.

Interface display

But the problem is that the interface (WinForm) shows Unicode, this time in order for our UI to display the Chinese characters properly, we need to replace \ \.

Looks like a replace can be done, not also!

Mode 1:

' \ \ ' character

"\" error. Blocked

Mode 2:

@ "\ \", "\\\\"

@ "\", "\ \"

This is the opposite.

Mode 3:

Customize an algorithm to complete the \ \ to \ Conversion.

Ideas:

Each character, in fact, is a number in the computer.

For example ' 1 ', the corresponding ASCII code is 49. For Chinese characters, there is GB2312 code, in the final analysis is a number. A Chinese character corresponds to a number (usually denoted by a 16 binary representation)

As long as the numerical value (19968) is calculated, such as \\u4e00, and the corresponding to the Chinese character can be. That is

char Hz = (char) 0x4e00

The next problem is to turn "\\u4e00" into 0x4e00, and the code is as follows:

        #region Handling Chinese//<summary>//\\u4e00-\\u9fa5-&GT;\U4E00-\U9FA5//</summary> <param name= "Val" ></param>///<returns></returns> protected static Strin            G Process_cn (String val) {string ret = val; if (Val.                IndexOf ("\\u") >-1) {int i =-1;                char[] Vals = new Char[4];                char Hz; while (ret. IndexOf ("\\u") >-1) {i = ret.                    IndexOf ("\\u"); if (i >-1 && i + 5 < ret. Length) {Vals = ret. Substring (i + 2, 4).                        ToCharArray ();                        Hz = (char) parseint (Vals); RET = ret. Replace ("\\u" + toString (vals), Hz.)                    ToString ());        }}} return ret;     }//<summary>//Char [], string   Self-reported parameter error (new String/string (char[]))//</summary>//<param name= "C" ></param> <returns></returns> protected static string toString (char[] c) {Stringbuilde            R sb = new StringBuilder (); Sb.            Append (c); Return SB.        ToString (); }//<summary>//(0x) 660e 26126//</summary>//<param name= "C" >            </param>//<returns></returns> protected static int parseint (char[] c) {            int n = 0; if (c.length = = 4) {n = parseint (c[3]) + parseint (c[2]) * + parseint (c[1]) * * + + Parse            Int (C[0]) * 16 * 16 * 16;        } return N; }///<summary>//(char) 0~9a~za~z (int) 0~15//</summary>//<param N Ame= "C" ></param>//<returns></returns> Protected Static int parseint (char c) {char[] arr1 = new char[] {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ',            ' 9 ', ' A ', ' B ', ' C ', ' d ', ' e ', ' f '};            char[] arr2 = new char[] {' A ', ' B ', ' C ', ' D ', ' E ', ' F '}; for (int i = 0; i < arr1. Length;            i++) {if (c = = Arr1[i]) return i; } for (int i = 0; i < arr2. Length;            i++) {if (c = = Arr2[i]) return i + 10;        } return-1;  } #endregion

  

C # Processing of Chinese in JSON-based communication

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.