Summary and realization of UrlEncode and UrlDecode

Source: Internet
Author: User
Tags urlencode

UrlEncode:
Used to encode URL strings, numbers and letters remain unchanged, spaces become ' + ', others (such as: Chinese characters) are first converted to hexadecimal notation, and then an identifier% is prepended to each byte.
For example: "Ah" ASCII hexadecimal is 0xb0a1-->%b0%a1

Code implementation:

1UnsignedCharChar_to_hex (unsignedCharch)2 {3     //0xa (10) converted to character ' A ' (65), plus//0x0 (0) translates to character ' 0 ' (48), plus4     return(unsignedChar) (Ch >9? CH + -: Ch + -);5 }6 7STD::stringUrlEncode (Const Char*PSRC)8 {9UnsignedCharchtemp;TenSTD::stringstrdest; One  A      while(*PSRC) -     { -Chtemp = (unsignedChar)*PSRC; the         if((Chtemp >='a'&& chtemp <='Z') || (Chtemp >='A'&& chtemp <='Z') || (Chtemp >='0'&& chtemp <='9') -|| STRCHR ("-_.! ~* ' ()", chtemp)) -         { -Strdest + =chtemp; +         } -          Else if(Chtemp = =' ') +          { A*pdest++ ='+'; at          } -         Else -         { -Strdest + ='%'; -Strdest + = Char_to_hex (unsignedChar) (Chtemp >>4) ); -Strdest + = Char_to_hex (unsignedChar) (Chtemp &0x0f) ); in         } -++PSRC; to     } +  -     returnstrdest; the}

UrlDecode:
Used to decode encoded URL strings, which are decoded in the opposite way as UrlEncode

Code implementation:

1UnsignedChar* UrlDecode (unsignedChar* PSRC, std::string&strdest)2 {3     Charsztemp[2];4 5      while(*PSRC)6     {7memset (Sztemp,'/0',2 );8         if(*psrc! ='%'&& *PSRC! =' ')9         {TenStrdest + = *psrc++; One             Continue; A         } -         Else if(*PSRC = =' ' ) -         { theStrdest + ='+'; -             Continue; -         } -         //Ignore% +++PSRC; -UnsignedCharCTMP =0; +  A          for(intK =0; K <2&& *psrc; ++k, ++PSRC)//example: Will%3f---> 0x3f at         { -             if(*psrc <='9')//0x30~0x39 -             { -CTMP |= (*PSRC &0x0f) << (1-K) *4; -             } -             Else if(*psrc >='a')//0x61~7a in             { -CTMP |= (*PSRC-0x57) << (1-K) *4; to             } +             Else//0x41~5a -             { theCTMP |= (*PSRC-0x37) << (1-K) *4; *             } $         }Panax NotoginsengStrdest + =cTmp; -     } the     return 0; +}

Summary and realization of UrlEncode and UrlDecode

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.