Encoding format conversion-URL encoding and decoding

Source: Internet
Author: User

As we all know, when searching for Chinese Characters in "Baidu.com", for example, "Old Three" Get the URL string: http://www.baidu.com/s? WD = % C0 % CF % C8 % FD & CL = 3, which is clearly encoded. What's going on?

Because some symbols cannot be directly transmitted in the URL, if you want to pass these special symbols in the URL, you need to use their encoding. The encoding format is % plus the characters' ASCII code, that is, a percent sign %, followed by the corresponding characters' ASCII (hexadecimal) code value. For example, the Space Encoding value is "% 20 ".
The following table lists some special URL symbols and codes.

      Hexadecimal value
1. + The plus sign in the URL indicates space. % 2B
2. Space Spaces in the URL can be encoded with a plus sign (+ ). % 20
3. / Separate directories and subdirectories % 2f
4. ? Separate actual URLs and Parameters % 3f
5. % Special characters % 25
6. # Indicates bookmarks % 23
7. & Delimiter between parameters specified in the URL % 26
8. = The value of the specified parameter in the URL. % 3d

So how do we encode and decode it? See the following code:

// Add system. Web reference first.
/// <Summary>
/// URL Decoding
/// </Summary>
/// <Param name = "Source"> uncode string </param>
/// <Param name = "encode"> encoding format of the string to be decoded </param>
/// <Returns> decoded string </returns>
Public static string urldecode (string source, encoding encode)
{
Return System. Web. httputility. urldecode (source, encode );
}
/// <Summary>
/// URL Encryption
/// </Summary>
/// <Param name = "Source"> string to be encrypted </param>
/// <Param name = "encode"> encoding format of the string to be encrypted </param>
/// <Returns> encrypted string </returns>
Public static string urlencode (string source, encoding encode)
{
Return System. Web. httputility. urlencode (source, encode );

}

Result of the old Three encoding conversion tool:

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.