C # Programming Summary (10) Character Transfer code

Source: Internet
Author: User

In order to adapt to some special needs, characters need to be transferred according to the rules, which facilitates transmission, display and other operations.

Look at the following transcoding, and you will know his usefulness.

1. String transcoding

The conversion is completed according to the original encoding format and the target encoding format. But may appear garbled oh. The previous chapter has already been introduced.

Code:

<summary>
        ///string encoding conversion
        ///</summary>
        ///<param name= "srcencoding" > Original code </param >
        ///<param name= "dstencoding" > Target code </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, dstencoding, srcbytes);
            Return dstencoding.getstring (bytes);
        }

Test Cases:

Input = "Welcome to the transcoding world!" ";
result = Transfer.transferencoding (Encoding.default, Encoding.UTF8, input);/Welcome to the world of transcoding

!
Console.WriteLine ("transferencoding results: {0}", result);
result = Transfer.transferencoding (Encoding.utf8,encoding.default,result);
Console.WriteLine ("TransferEncoding Reverse code results: {0}", result);/Welcome to the transcoding World

!

2. HTML transcoding

Important: Characters < and > are encoded as &lt; when embedded in a block of text and &gt;

If characters such as whitespace and punctuation are passed in the HTTP stream, they may be incorrectly interpreted at the receiving end. HTML encoding converts characters that are not allowed in HTML into equivalent character entities, and HTML decoding reverses this encoding process. For example, for HTTP transmissions, characters < and > are encoded as &lt; when embedded in a text block and &gt;. To encode or decode values other than the WEB application, use the Webutility class.

Transfer 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);   

         
        }

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);  
        }

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.