C # programming Summary (10) character Transcoding

Source: Internet
Author: User
Tags html encode html decode url decode

To meet certain special needs, characters must be transcoded according to rules to facilitate transmission, presentation, and other operations. Let's take a look at the following transcoding to find out its usefulness. 1. String transcoding completes Conversion Based on the original encoding format and the target encoding format. However, garbled characters may occur. The previous chapter has already been introduced. Code: copy the 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> string </returns> public static string TransferEncoding (Encoding srcEncoding, encoding dstEncoding, string srcStr) {byte [] srcBytes = srcEncoding. getBytes (srcStr); byte [] bytes = Encoding. convert (srcEncoding, dstEnc Oding, srcBytes); return dstEncoding. GetString (bytes);} copy the code test case: input = "welcome to the transcoding world! "; Result = Transfer. TransferEncoding (Encoding. Default, Encoding. UTF8, input); // welcome to the transcoding world! Console. writeLine ("TransferEncoding result: {0}", result); result = Transfer. transferEncoding (Encoding. UTF8, Encoding. default, result); Console. writeLine ("TransferEncoding anti-transcoding result: {0}", result); // welcome to the transcoding world! 2. Html transcoding key points: characters <and> are encoded as & lt; and & gt when they are embedded into text blocks. If blank and punctuation characters are transmitted in the HTTP stream, they may be incorrectly interpreted at the receiving end. HTML encoding converts a character that is not allowed in HTML to an equivalent entity. HTML Decoding reverses this encoding process. For example, for HTTP transmission, characters <and> are encoded as & lt; and & gt; when embedded in a text block ;. To encode or decode values other than Web applications, use the WebUtility class. Transcoding: copy the code /// <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);} duplicate code decoding: copy the code /// <summary> /// html Decoding /// </summary> /// <param name = "html"> </param> /// <returns> </returns> public static string HtmlDecode (string html) {return HttpUt Ility. htmlDecode (html); // System. net. webUtility. htmlDecode (html);} copy the code test case: copy the code // HtmlEncode test result = string. empty; input = "

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.