Reprinted from: http://www.cnblogs.com/yank/p/3536863.htmlC# Programming Summary (10) Character transcoding
To accommodate a particular need, characters need to be transcoded according to rules for easy transmission, presentation, and other operations.
Look at the transcoding below and you'll know what he's for.
1, String transcoding
The conversion is completed according to the original encoding format and the target encoding format. However, there may be garbled. The previous chapter has been introduced.
Code:
<summary>/// string encoding conversion/// </summary>/ <param name= "srcencoding" > Original encoding </param >// <param name= "dstencoding" > Target encoding </param>// <param name= "Srcbytes" > Original string </ Param> //<returns> strings </returns> public static string transferencoding (Encoding srcencoding , Encoding dstencoding, string srcstr) { byte[] srcbytes = srcencoding.getbytes (SRCSTR); byte[] bytes = Encoding.convert (srcencoding, dstencoding, srcbytes); Return dstencoding.getstring (bytes); }
Test Case:
Input = "Welcome to transcoding world!" "; result = Transfer.transferencoding (Encoding.default, Encoding.UTF8, input);//Welcome to transcoding World! Console.WriteLine ("transferencoding result: {0}", result); result = Transfer.transferencoding (Encoding.utf8,encoding.default,result); Console.WriteLine ("TransferEncoding reversal code result: {0}", result);//Welcome to transcoding World!
2. HTML transcoding
Important: Encode characters < and > into a text block when encoded as < and >
If characters such as whitespace and punctuation are passed in the HTTP stream, they may be interpreted incorrectly at the receiving end. HTML encoding converts characters that are not allowed in HTML to equivalent character entities, and HTML decoding reverses the encoding process. For example, for HTTP transmission, character < and > are encoded as < when embedded in a block of text and >.
To encode or decode values outside the WEB application, use the Webutility class.
Transcoding:
<summary>//HTML transcoding/// </summary>/ <param name= "html" ></param> // <returns></returns> public static string HtmlEncode (string html) { return Httputility.htmlencode (HTML);//system.net.webutility.htmlencode (HTML); }
Decoding:
<summary>//HTML decoding///</summary>// <param name= "html" ></param> //<returns></returns> public static string HtmlDecode (string html) { return Httputility.htmldecode (HTML);//system.net.webutility.htmldecode (HTML); }
Test Case:
HTMLEncode Test result = string. Empty; Input = "
3. URL encodingImportant: Escape the special characters of the URL to make it legitimate.
A method that can be used to encode an entire URL, including query string values. If characters such as whitespace and punctuation are passed in the HTTP stream, they may be interpreted incorrectly at the receiving end. URL encoding converts characters that are not allowed in URLs to equivalent character entities, and URL decoding reverses this encoding process. For example, when embedded in a block of text to be transferred in a URL, the characters < and > are encoded as%3C and%3e, respectively.
By default, the Httputility.urlencode method uses UTF-8 encoding. Therefore, use the UrlEncode method to provide the same result using the UrlEncode method and specify UTF8 as the second parameter.
UrlEncode is a convenient way to access the UrlEncode method from an ASP. NET application at run time. Internally, UrlEncode uses the UrlEncode method to enter a string.
To encode or decode values outside the WEB application, use the Webutility class.
HttpUtility namespace: system.web
URL transcoding
<summary>///URL transcoding/// </summary>/ <param name= "url" ></param> // <returns></returns> public static string UrlEncode (string url) { return Httputility.urlencode (URL); }
URL decoding
<summary>///URL decoding///</summary>// <param name= "url" ></param> // <returns></returns> public static string UrlDecode (string url) { return Httputility.urldecode (URL); }
Test Case:
UrlEncode Test input = "Http://www.baidu.com?username=<find>&content=ab C"; result = Transfer.urlencode (input);//Result: Http%3a%2f%2fwww.baidu.com%3fusername%3d%3cfind%3e%26content%3dab+c Console.WriteLine ("Url Encode: {0}", result); result = Transfer.urldecode (result);//Result: Http://www.baidu.com?username=<find>&content=ab C Console.WriteLine ("Url Decode: {0}", result);
4, Base64The Base64 content delivery code is designed to describe a 8-bit byte of any sequence as a form that is not easy to be recognized directly.
Important: Re-encode the data using 64 basic ASCII characters for encryption and transmission.
Encoding Rules
The idea of BASE64 encoding is to re-encode the data using 64 basic ASCII characters. It splits the data that needs to be encoded into a byte array. A group of 3 bytes. The 24-bit data is sorted sequentially, and the 24 bits of data are divided into 4 groups, 6 bits per group. Then fill in a byte with two 0 in front of the highest bit of each group. This re-encodes a 3-byte set of data into 4 bytes. When the number of bytes of data to encode is not an integral multiple of 3, that is, the last group is less than 3 bytes at the time of grouping. At this point the last group is populated with 1 to 2 0 bytes. and add 1 to 2 "=" At the end after the last encoding is complete.
Example: ABC will be BASE64 encoded:
1, first take the ASCII code value of the ABC corresponding. A (+) B (+) C (67);
2, then take the binary value A (01000001) B (01000010) C (01000011);
3, then the three bytes of the binary code to connect (010000010100001001000011);
4, then 6 bits of the unit into 4 data blocks, and the highest bit filled two 0 after the formation of 4 bytes of the encoded value, (00010000) (00010100) (00001001) (00000011), where the blue part of the real data;
5, then the four bytes of data into 10 binary numbers (16) (20) (9) (3);
6, finally according to the BASE64 given 64 basic character table, the corresponding ASCII code character (Q) (U) (J) (D), where the value is actually the data in the character list index.
BASE64 Character Tabulation :
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/
Application:
1. BASE64 encoding can be used to pass longer identity information in HTTP environment
2, Base64 also often used as a simple "encryption" to protect certain data, such as URLs, and real encryption is often cumbersome.
Code implementation
Base64 transcoding
<summary>//Base64 transcoding/// </summary>/ <param name= "input" ></param> <returns></returns> public static string ToBase64 (string input) { byte[] bytes = Encoding.UTF8.GetBytes (input); Return convert.tobase64string (bytes); }
Base64 decoding:
<summary>//Base64 string decoding///</summary>// <param name= "Input" ></param> //<returns></returns> public static string FromBase64 (string input) { byte[] bytes = Convert.frombase64string (input); Return convert.tobase64string (bytes); }
Test Case:
Base64 transcode Test input = "Coming from the new world!"; result = Transfer.tobase64 (input); Result: Q29taw5nigzyb20gdghlig5ldyb3b3jszce= Console.WriteLine ("ToBase64: {0}", result); result = Transfer.frombase64 (result); Results: Coming from the new world! Console.WriteLine ("FromBase64: {0}", result);
5, BitconverterConverts the underlying data type to and from a byte array.
The Bitconverter class helps manipulate value types in a basic form in a series of bytes. A byte is defined as a 8-bit unsigned integer. As shown in the following table, the Bitconverter class includes converting each primitive type to a byte array with a static method and converting a byte array to its primitive type.
Type |
Convert to bytes |
Convert from byte |
Boolean |
GetBytes (Boolean) |
ToBoolean |
Char |
GetBytes (Char) |
ToChar |
Double |
GetBytes (Double) Or Doubletoint64bits (Double) |
ToDouble Or Int64bitstodouble |
Int16 |
GetBytes (Int16) |
ToInt16 |
Int32 |
GetBytes (Int32) |
ToInt32 |
Int64 |
GetBytes (Int64) |
ToInt64 |
Single |
GetBytes (Single) |
ToSingle |
UInt16 |
GetBytes (UInt16) |
ToUInt16 |
UInt32 |
GetBytes (UInt32) |
ToUInt32 |
UInt64 |
GetBytes (UInt64) |
ToUInt64 |
Here we take the most basic string type as an example.
Byte is converted to string
<summary>//// byte array to string/// Converts the numeric value of each element of the specified byte array to its equivalent hexadecimal string representation. //</summary>// <param name= "bytes" ></param>// <returns></returns> Public static string bittostring (byte[] bytes) { if (bytes = = null) { return null; } Converts the numeric value of each element of the specified byte array to its equivalent hexadecimal string representation. return bitconverter.tostring (bytes); }
hexadecimal string to Byte
<summary>///convert hexadecimal string to byte array///</summary>// <param name= "Bitstr" ></param > //<returns></returns> public static byte[] Frombitstring (string bitstr) { if ( BITSTR = = null) { return null; } string[] SInput = Bitstr.split ("-". ToCharArray ()); byte[] data = new Byte[sinput.length]; for (int i = 0; i < sinput.length; i++) { data[i] = byte. Parse (Sinput[i], numberstyles.hexnumber); } return data; }
Test Case:
Bit transcoding input = "Coming from the new world!"; result = transfer.bittostring (Encoding.UTF8.GetBytes (input)); Results: 43-6f-6d-69-6e-67-20-66-72-6f-6d-20-74-68-65-20-6e-65-77-20-77-6f-72-6c-64-21 Console.WriteLine (" Bittostring: {0} ", result); result = Encoding.UTF8.GetString (transfer.frombitstring (result)); Results: Coming from the new world! Console.WriteLine ("Frombitstring: {0}", result);
Run:C # Programming Summary (10) Character transcoding