Url解码代码
Url编码与解码的代码,使用WTL的CString,支持日文字符~~ by 飘飘白云(http://blog.youkuaiyun.com/kesalin)
- CStringWConvertUTF8ToUTF16(constCStringA&utf8)
- {
- intwLen=MultiByteToWideChar(CP_UTF8,0,utf8,utf8.GetLength(),0,0);
- CStringWbuf;
- WCHAR*dd=buf.GetBuffer(wLen);
- wLen=MultiByteToWideChar(CP_UTF8,0,utf8,utf8.GetLength(),dd,wLen);
- buf.ReleaseBuffer(wLen);
- returnbuf;
- }
- CStringWDecodeUrl(conststd::string&src)
- {
- CStringWdstUrlW(L"");
- intsrclen=(int)src.size();
- std::stringdst;
- for(inti=0;i<srclen;i++){
- if(src[i]=='%'){
- if(isxdigit(src[i+1])&&isxdigit(src[i+2])){
- charc1=src[++i];
- charc2=src[++i];
- c1=c1-48-((c1>='A')?7:0)-((c1>='a')?32:0);
- c2=c2-48-((c2>='A')?7:0)-((c2>='a')?32:0);
- dst+=(unsignedchar)(c1*16+c2);
- }
- }
- elseif(src[i]=='+'){
- dst+='';
- }
- else{
- dst+=src[i];
- }
- }
- if(dst.size()>0){
- dstUrlW=ConvertUTF8ToUTF16(CStringA(dst.c_str()));
- }
- returndstUrlW;
- }
- voidEncodeUrl(char*dst,constchar*src)
- {
- while(*src){
- unsignedcharc=static_cast<unsignedchar>(*src);
- if(c==''){
- *dst++='+';
- }
- elseif(isalnum(c)||c=='$'||c=='-'||c=='_'||c=='.'||
- c=='!'||c=='*'||c=='/''||c=='('||c==')'||c==','){
- *dst++=c;
- }
- else{
- *dst++='%';
- unsignedcharnibble=(c>>4)&0xF;
- *dst++=(nibble<10?nibble+'0':nibble-10+'A');
- nibble=c&0xF;
- *dst++=(nibble<10?nibble+'0':nibble-10+'A');
- }
- src++;
- }
- *dst='/0';
- }
1130

被折叠的 条评论
为什么被折叠?



