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