将字符串URL编码

 1 int URLEncode( const CString& rawStr,CString& encodeStr )
 2 {
 3 #ifdef _UNICODE
 4     std::string strPre = wstring2string((LPCTSTR)rawStr).c_str();
 5     std::string strEncoded;
 6     char*   szHexTable = "0123456789ABCDEF",
 7         *    szUnsafeTable = "\"<>%\\^[]`+$,@:;/!#?=&" ;
 8     for (int i=0 ; i < strPre.size(); i++){
 9         if (((unsigned char)strPre[i] > 32) && ((unsigned char)strPre[i] < 123) && ( strchr(szUnsafeTable,(unsigned char)strPre[i])== NULL )){
10             strEncoded += (unsigned char)strPre[i] ;
11         }
12         else{
13             strEncoded += "%" ;
14             strEncoded += szHexTable[(unsigned char)strPre[i] / 16] ;
15             strEncoded += szHexTable[(unsigned char)strPre[i] % 16] ;
16         }
17     }
18 
19     encodeStr = string2wstring(strEncoded).c_str();
20     return encodeStr.GetLength() ;
21 #else
22     ASSERT(FALSE);
23 #endif
24 
25 }

只是简略实现,不确定@等符号是否需要转码。

posted @ 2013-10-23 20:43  ssp1024  阅读(635)  评论(0编辑  收藏  举报