VC URLEncode & UrlDecode

來源:互聯網
上載者:User

本文轉載自:http://hi.baidu.com/usoa/blog/item/0f1ad1ce35e6fa0f93457e6a.html

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);}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.