http://blog.csdn.net/leechiyang/archive/2008/02/22/2112915.aspx
http://blog.csdn.net/zhengyun_ustc/archive/2002/05/20/12654.aspx
URLEncode: inline BYTE toHex(const BYTE &x) { return x > 9 ? x + 55: x + 48; }
CString URLEncode(CString sIn) { CString sOut; const int nLen = sIn.GetLength() + 1; register LPBYTE pOutTmp = NULL; LPBYTE pOutBuf = NULL; register LPBYTE pInTmp = NULL; LPBYTE pInBuf =(LPBYTE)sIn.GetBuffer(nLen); BYTE b = 0;
//alloc out buffer pOutBuf = (LPBYTE)sOut.GetBuffer(nLen*3 - 2);//new BYTE [nLen * 3];
if(pOutBuf) { pInTmp = pInBuf; pOutTmp = pOutBuf;
// do encoding while (*pInTmp) { if(isalnum(*pInTmp)) *pOutTmp++ = *pInTmp; else if(isspace(*pInTmp)) *pOutTmp++ = '+'; else { *pOutTmp++ = '%'; *pOutTmp++ = toHex(*pInTmp>>4); *pOutTmp++ = toHex(*pInTmp%16); } pInTmp++; } *pOutTmp = '\0'; //sOut=pOutBuf; //delete [] pOutBuf; sOut.ReleaseBuffer(); } sIn.ReleaseBuffer(); return sOut; }
UrlDecode: #define IsHexNum(c) ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'))
CString Utf8ToStringT(LPSTR str) { _ASSERT(str); USES_CONVERSION; WCHAR *buf; int length = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0); buf = new WCHAR[length+1]; ZeroMemory(buf, (length+1) * sizeof(WCHAR)); MultiByteToWideChar(CP_UTF8, 0, str, -1, buf, length);
return (CString(W2T(buf))); }
CString UrlDecode(LPCTSTR url) { _ASSERT(url); USES_CONVERSION; LPSTR _url = T2A(const_cast<LPTSTR>(url)); int i = 0; int length = (int)strlen(_url); CHAR *buf = new CHAR[length]; ZeroMemory(buf, length); LPSTR p = buf; while(i < length) { if(i <= length -3 && _url[i] == '%' && IsHexNum(_url[i+1]) && IsHexNum(_url[i+2])) { sscanf(_url + i + 1, "%x", p++); i += 3; } else { *(p++) = _url[i++]; } } return Utf8ToStringT(buf);
#define IsHexNum(c) ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'))
CString Utf8ToStringT(LPSTR str) { _ASSERT(str); USES_CONVERSION; WCHAR *buf; int length = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0); buf = new WCHAR[length+1]; ZeroMemory(buf, (length+1) * sizeof(WCHAR)); MultiByteToWideChar(CP_UTF8, 0, str, -1, buf, length);
return (CString(W2T(buf))); } /**//* CString UrlDecode(LPCTSTR url) { _ASSERT(url); USES_CONVERSION; LPSTR _url = T2A(const_cast<LPTSTR>(url)); int i = 0; int length = (int)strlen(_url); CHAR *buf = new CHAR[length]; ZeroMemory(buf, length); LPSTR p = buf; while(i < length) { if(i <= length -3 && _url[i] == '%' && IsHexNum(_url[i+1]) && IsHexNum(_url[i+2])) { sscanf(_url + i + 1, "%x", p++); i += 3; } else { *(p++) = _url[i++]; } } //return Utf8ToStringT(buf); return CString(buf); } */ CString UrlDecode(LPCTSTR url) { _ASSERT(url); USES_CONVERSION; LPSTR _url = T2A(const_cast<LPTSTR>(url)); int i = 0; int length = (int)strlen(_url); CHAR *buf = new CHAR[length]; ZeroMemory(buf, length); LPSTR p = buf; char tmp[4]; while(i < length) { if(i <= length -3 && _url[i] == '%' && IsHexNum(_url[i+1]) && IsHexNum(_url[i+2])) { memset(tmp, 0, sizeof(tmp)); memcpy(tmp, _url + i + 1,2); sscanf(tmp, "%x", p++); i += 3; } else { *(p++) = _url[i++]; } } return Utf8ToStringT(buf); }
void ConvertUtf8ToGBK(CString& strUtf8) { int len=MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUtf8, -1, NULL,0); unsigned short * wszGBK = new unsigned short[len+1]; memset(wszGBK, 0, len * 2 + 2); MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUtf8, -1, wszGBK, len); len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL); char *szGBK=new char[len + 1]; memset(szGBK, 0, len + 1); WideCharToMultiByte (CP_ACP, 0, wszGBK, -1, szGBK, len, NULL,NULL);
strUtf8 = szGBK; delete[] szGBK; delete[] wszGBK; }
void ConvertGBKToUtf8(CString& strGBK) { int len=MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, NULL,0); unsigned short * wszUtf8 = new unsigned short[len+1]; memset(wszUtf8, 0, len * 2 + 2); MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, wszUtf8, len);
len = WideCharToMultiByte(CP_UTF8, 0, wszUtf8, -1, NULL, 0, NULL, NULL); char *szUtf8=new char[len + 1]; memset(szUtf8, 0, len + 1); WideCharToMultiByte (CP_UTF8, 0, wszUtf8, -1, szUtf8, len, NULL,NULL);
strGBK = szUtf8; delete[] szUtf8; delete[] wszUtf8; }
void UTF_8ToUnicode(WCHAR* pOut,char *pText) { char* uchar = (char *)pOut; uchar[1] = ((pText[0] & 0x0F) << 4) + ((pText[1] >> 2) & 0x0F); uchar[0] = ((pText[1] & 0x03) << 6) + (pText[2] & 0x3F); return; }
// Unicode 转换成UTF-8 void UnicodeToUTF_8(char* pOut,WCHAR* pText) { // 注意 WCHAR高低字的顺序,低字节在前,高字节在后 char* pchar = (char *)pText; pOut[0] = (0xE0 | ((pchar[1] & 0xF0) >> 4)); pOut[1] = (0x80 | ((pchar[1] & 0x0F) << 2)) + ((pchar[0] & 0xC0) >> 6); pOut[2] = (0x80 | (pchar[0] & 0x3F)); return; }
// 把Unicode 转换成 GB2312 void UnicodeToGB2312(char* pOut,unsigned short uData) { WideCharToMultiByte(CP_ACP,NULL,&uData,1,pOut,sizeof(WCHAR),NULL,NULL); return; }
// GB2312 转换成 Unicode void Gb2312ToUnicode(WCHAR* pOut,char *gbBuffer) { ::MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,gbBuffer,2,pOut,1); return; }
//GB2312 转为 UTF-8 void GB2312ToUTF_8(CString& pOut,char *pText, int pLen) { char buf[1024]; char* rst = new char[pLen + (pLen >> 2) + 2]; memset(buf,0,1024); memset(rst,0,pLen + (pLen >> 2) + 2); int i = 0; int j = 0; while(i < pLen) { //如果是英文直接复制就可以 if( *(pText + i) >= 0) { rst[j++] = pText[i++]; } else { WCHAR pbuffer; Gb2312ToUnicode(&pbuffer,pText+i);
UnicodeToUTF_8(buf,&pbuffer);
unsigned short int tmp = 0; tmp = rst[j] = buf[0]; tmp = rst[j+1] = buf[1]; tmp = rst[j+2] = buf[2];
j += 3; i += 2; } } strcpy(&rst[j],"\0"); //返回结果 pOut = rst; delete []rst;
return; }
//UTF-8 转为 GB2312 void UTF_8ToGB2312(CString &pOut, char *pText, int pLen) { char * newBuf = new char[pLen]; char Ctemp[4]; memset(Ctemp,0,4); int i =0; int j = 0; while(i < pLen) { if(pText[i] > 0) { newBuf[j++] = pText[i++]; } else { WCHAR Wtemp; UTF_8ToUnicode(&Wtemp,pText + i);
UnicodeToGB2312(Ctemp,Wtemp);
newBuf[j] = Ctemp[0]; newBuf[j + 1] = Ctemp[1];
i += 3; j += 2; } } strcpy(&newBuf[j],"\0"); pOut = newBuf; delete []newBuf; return; }
CString UTF8_Encode(LPTSTR strUnicode) { long TLen ; CString UTF8_EncodeLong ;
TLen = CString(strUnicode).GetLength() ;
if(TLen == 0) { return CString(strUnicode); }
long lngBufferSize ; long lngResult ;
//Set buffer for longest possible string. lngBufferSize = TLen * 3 + 1 ; char *bytUtf8 = new char[lngBufferSize] ;
//Translate using code page 65001(UTF-8).
lngResult = WideCharToMultiByte(CP_UTF8, 0, (unsigned short*)strUnicode, TLen, bytUtf8, lngBufferSize, NULL, 0) ;
bytUtf8[lngResult] = NULL ;
return CString(bytUtf8) ; } /**//*************************************************************************/ inline BYTE toHex(const BYTE &x) { return x > 9 ? x + 55: x + 48; }
CString URLEncode(CString sIn) { CString sOut; const int nLen = sIn.GetLength() + 1; register LPBYTE pOutTmp = NULL; LPBYTE pOutBuf = NULL; register LPBYTE pInTmp = NULL; LPBYTE pInBuf =(LPBYTE)sIn.GetBuffer(nLen); BYTE b = 0;
//alloc out buffer pOutBuf = (LPBYTE)sOut.GetBuffer(nLen*3 - 2);//new BYTE [nLen * 3];
if(pOutBuf) { pInTmp = pInBuf; pOutTmp = pOutBuf;
// do encoding while (*pInTmp) { if(isalnum(*pInTmp)) *pOutTmp++ = *pInTmp; else if(isspace(*pInTmp)) *pOutTmp++ = '+'; else { *pOutTmp++ = '%'; *pOutTmp++ = toHex(*pInTmp>>4); *pOutTmp++ = toHex(*pInTmp%16); } pInTmp++; } *pOutTmp = '\0'; //sOut=pOutBuf; //delete [] pOutBuf; sOut.ReleaseBuffer(); } sIn.ReleaseBuffer(); return sOut; }
} |