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 < when embedded in a block of text and >
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 < when embedded in a text block and >. 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);
}