C # convert Chinese characters to Unicode encoding,

Source: Internet
Author: User
Tags printable characters

C # convert Chinese characters to Unicode encoding,

It is mainly used to convert Chinese characters into Unicoude encoding when generating json format to prevent page garbled characters.

   protected string GetUnicode(string text)    {        string result = "";        for (int i = 0; i < text.Length; i++)        {            if ((int)text[i] > 32 && (int)text[i] < 127)            {                result += text[i].ToString();            }            else                result += string.Format("\\u{0:x4}", (int)text[i]);        }        return result;    }

 

Running result:

 

 

Knowledge points involved:

1. "x" indicates hexadecimal, and "x4" indicates the controllable length in hexadecimal format. If the length is insufficient, fill it with the leading 0.

2. Unicode writing: When a Unicode character is expressed, the character is usually expressed with "U +" followed by a group of hexadecimal numbers.

3. ASCII Code (American Standard Code for Information Interchange)

The basic ASCII character set contains a total of 128 characters, including 96 printable characters, including commonly used letters, numbers, punctuation marks, and 32 control characters.

  • 0 ~ 31 and 127 (33 in total) are control characters or communication special characters (others can be displayed), such as control characters: LF (line feed), CR (Press ENTER), FF (page feed) DEL (delete), BS (return), BEL (zhenling), etc. Special communication characters: SOH (Text header), EOT (Text tail), ACK (OK), etc; the ASCII values are 8, 9, 10, and 13, respectively, and are converted to backspace, tabulation, line feed, and carriage return characters. They do not have specific graphic display, but will have different effects on text display according to different applications.
  • 32 ~ 126 (95 characters in total) is a character (32 SP is a space), of which 48 ~ 57 is 0 to 9 ten Arabic numerals, 65 ~ 90 is 26 uppercase English letters, 97 ~ 122 contains 26 lower-case letters and some punctuation marks and operator numbers.

 

References:

1. C # String. Format usage and Format description

Http://www.cnblogs.com/gongth-12/archive/2012/11/26/2791642.html#undefined


Symbol in C Language <Yes

Left shift operator (<)

Removes all the binary bits of an operation object from the left and adds 0 to the right ).

For example, a = a <2 shifts the binary bits of a two places to the left and complements 0 to the right,

Move 1 to the left and then a = a * 2;

If the left shift does not include 1 in the Discard high position, then shifts one bit left, which is equivalent to multiplying the number by 2.
Shift right operator (>)

Shifts all the binary bits of a number to several places to the right, and adds 0 to the left of the positive number, 1 to the left of the negative number, and discards the right of the negative number.

The operand shifts one digit to the right, which is equivalent to dividing the number by 2.

For example, a = a> 2 shifts the binary bit of a two places to the right,

0 or 1 to see whether the number is positive or negative.

Symbol in C Language <Yes

Left shift operator (<)

Removes all the binary bits of an operation object from the left and adds 0 to the right ).

For example, a = a <2 shifts the binary bits of a two places to the left and complements 0 to the right,

Move 1 to the left and then a = a * 2;

If the left shift does not include 1 in the Discard high position, then shifts one bit left, which is equivalent to multiplying the number by 2.
Shift right operator (>)

Shifts all the binary bits of a number to several places to the right, and adds 0 to the left of the positive number, 1 to the left of the negative number, and discards the right of the negative number.

The operand shifts one digit to the right, which is equivalent to dividing the number by 2.

For example, a = a> 2 shifts the binary bit of a two places to the right,

0 or 1 to see whether the number is positive or negative.

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.