MFC URL编码(UTF8和GB2312)


// 进行Url编码 UTF-8 
CString UrlEncode(CString strUnicode)
{
	LPCWSTR unicode = T2CW(strUnicode);
	 int len = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, 0, 0, 0, 0);
	 if (!len)
	  return strUnicode;
		 char *utf8 = new char[len + 1];
	 char *utf8temp = utf8;
	 WideCharToMultiByte(CP_UTF8, 0, unicode, -1, utf8, len + 1, 0, 0);
	 utf8[len] = NULL;   
	 CString strTemp, strEncodeData; 
	 while (*utf8 != '/0') 
	 { 
	  strTemp.Format(_T("%%%2x"), (BYTE)*utf8); 
	  strEncodeData += strTemp; 
	  ++utf8; 
	 } 

	 delete []utf8temp;
	
	 return CString(strEncodeData);

}
// 进行Url编码 GB2312
CString UrlEncodeGB(CString sIn){
	string dd; 
	char* str=GetAnsiString(sIn);
	size_t len = strlen(str);
    for (size_t i=0;i>4,((BYTE*)str)[i] %16);
            dd.append(tempbuff);
        }

    }
	CString mstr(dd.c_str());
	return mstr;


}
//unicode下将wchar_t*转换成char*
char* GetAnsiString(const CString &s) 
{  
	int nLen=0;	
	int nSize = s.GetLength(); 
	for(int i=0;i=19968 && k<=40869){
			nLen+=2;
		}else{
			nLen++;
		}
	}
	char *pAnsiString = new char[nLen+1];  
	wcstombs_s(pAnsiString, s, nLen+1);  
	return pAnsiString;  
}


你可能感兴趣的:(utf8)