C # convert a string (containing Chinese characters) to hexadecimal encoding ),

Source: Internet
Author: User
c # string (containing Chinese characters) into hexadecimal encoding (transform),

public static string Str2Hex (string s)
        {
            string result = string.Empty;
 
            byte [] arrByte = System.Text.Encoding.GetEncoding ("GB2312"). GetBytes (s);
            for (int i = 0; i <arrByte.Length; i ++)
            {
                result + = "& # x" + System.Convert.ToString (arrByte [i], 16) + ";"; //Convert.ToString(byte, 16) converts byte to hexadecimal string
            }
 
            return result;
        }
 
 
Into the kind of hexadecimal code that can be transmitted on the Internet, similar to% 8D% E2? In this case,
Use System.Web.HTTPUtility.URLEncode ().
  
  To convert from Guangguang decimal to hexadecimal, you can use bytes (i) .ToString ("X"),
This is to convert a byte into a hexadecimal string. "X" means uppercase hexadecimal characters. Use "x" to get lowercase.
 
 
reference
String (containing Chinese characters) into ascII hexadecimal problem
http://topic.csdn.net/t/20040905/22/3342635.html
 
Coded decoding
http://xiaodi.cnblogs.com/archive/2005/04/26/145493.aspx
 
 
 
        public string EncodingSMS (string s)
        {
            string result = string.Empty;
 
            byte [] arrByte = System.Text.Encoding.GetEncoding ("GB2312"). GetBytes (s);
            for (int i = 0; i <arrByte.Length; i ++)
            {
                result + = System.Convert.ToString (arrByte [i], 16); //Convert.ToString(byte, 16) converts byte to hexadecimal string
            }
 
            return result;
        }
 
        public string DecodingSMS (string s)
        {
            string result = string.Empty;
 
            byte [] arrByte = new byte [s.Length / 2];
            int index = 0;
            for (int i = 0; i <s.Length; i + = 2)
            {
                arrByte [index ++] = Convert.ToByte (s.Substring (i, 2), 16); //Convert.ToByte(string,16) converts a hex string into a byte
            }
            result = System.Text.Encoding.Default.GetString (arrByte);
 
            return result;
 
        }
 
 
The rules for encoding and decoding are as follows:
When encoding, all characters in the string are converted into the hexadecimal value of the corresponding ASCII value. For example, the ASCII value of "A" is 65, and the hexadecimal value is 41. Therefore, two characters " 41 "to represent the character" A ".
For Chinese characters, it is expressed by the hexadecimal value of its internal code. For example, "test" should be: B2E2CAD4.
 
 
Principle:
 
 
            string aaa = "AB test";
            byte [] bbb = System.Text.Encoding.Default.GetBytes (aaa);
            string ccc = System.Text.Encoding.Default.GetString (bbb);
 
            for (int i = 0; i <bbb.Length; i ++)
            {
                Response.Write (System.Convert.ToString (bbb [i], 16));
            }
            Response.Write (ccc);

How to use ^ in C
a1 = 0x01; // 0000 0001
a2 = 0x00; // 0000 0000
a3 = 0x03; // 0000 0011
a4 = 0x02; // 0000 0010

b1 = a1 ^ a2; // 0000 0001
b2 = a1 ^ a3; // 0000 0010
b3 = a1 ^ a4; // 0000 0011

^ XOR operator, the same bit value is 0, the difference is 1, see the example above.

//
Examples of simple practical problems:
====== \ ================
====== a ======== b =======
The above are 2 circuits, the two switches are a and b respectively, open state: \ [1], closed state: / [0].
If both are turned on or off at the same time, neither circuit is connected.
If a is turned on [1], b is turned off [0], circuit 1 is energized
====== \ ======== / =======
If a is closed [0], b is open [1], circuit 2 is energized
====== / ======= \ =======
In summary, the circuit does not work when the states of a and b are the same [0], and the circuit is energized when a and b are not the same [1].
 
How to use ^ in C

a1 = 0x01; // 0000 0001
a2 = 0x00; // 0000 0000
a3 = 0x03; // 0000 0011
a4 = 0x02; // 0000 0010

b1 = a1 ^ a2; // 0000 0001
b2 = a1 ^ a3; // 0000 0010
b3 = a1 ^ a4; // 0000 0011

^ XOR operator, the same bit value is 0, the difference is 1, see the example above.

//
Examples of simple practical problems:
====== \ ================
====== a ======== b =======
The above are 2 circuits, the two switches are a and b respectively, open state: \ [1], closed state: / [0].
If both are turned on or off at the same time, neither circuit is connected.
If a is turned on [1], b is turned off [0], circuit 1 is energized
====== \ ======== / =======
If a is closed [0], b is open [1], circuit 2 is energized
====== / ======= \ =======
In summary, the circuit does not work when the states of a and b are the same [0], and the circuit is energized when a and b are not the same [1].
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.