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
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.
^ 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].
^ 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].
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.