VC++ 字符CString 转换为 UTF-8 的 URLEncode解决方法

VC++ 字符CString 转换为 UTF-8 的 URLEncode
///%E6%B1%89%E5%AD%97%31%32%33%61%62%63
///汉字123abc
CString input = _T("汉字123abc");

我想通过 一个函数 把 input 通过 UTF-8编码 的URLEncode 得到 %E6%B1%89%E5%AD%97%31%32%33%61%62%63

步骤 是把 每一个字符对应一个 UTF-8编码 例如此例子中:
汉: E6 B1 89
字: E5 AD 97
1: 31
2: 32
3: 33
a: 61
b: 62
c: 63

我该怎么去写这样一个函数了? 


------解决方案--------------------
如果是Unicode,则CString保存的就是UNICODE编码; 否则就是ANSI编码。
楼主只需要进行编译转换,类似:
C/C++ code

#include <windows.h>

#include <iostream>

#include <vector>



using namespace std;



std::wstring UT2WC(const char* buf)

{

    int len = MultiByteToWideChar(CP_UTF8, 0, buf, -1, NULL, 0);

    std::vector<wchar_t> unicode(len);

    MultiByteToWideChar(CP_UTF8, 0, buf, -1, &unicode[0], len);



    return std::wstring(&unicode[0]);

}



std::string WC2UT(const wchar_t* buf)

{

    int len = WideCharToMultiByte(CP_UTF8, 0, buf, -1, NULL, 0, NULL, NULL);

    std::vector<char> utf8(len);

    WideCharToMultiByte(CP_UTF8, 0, buf, -1, &utf8[0], len, NULL, NULL);



    return std::string(&utf8[0]);

}



std::wstring MB2WC(const char* buf)

{

    int len = MultiByteToWideChar(CP_ACP, 0, buf, -1, NULL, 0);

    std::vector<wchar_t> unicode(len);

    MultiByteToWideChar(CP_ACP, 0, buf, -1, &unicode[0], len);



    return std::wstring(&unicode[0]);

}



std::string WC2MB(const wchar_t* buf)

{

    int len = WideCharToMultiByte(CP_ACP, 0, buf, -1, NULL, 0, NULL, NULL);

    std::vector<char> utf8(len);

    WideCharToMultiByte(CP_ACP, 0, buf, -1, &utf8[0], len, NULL, NULL);



    return std::string(&utf8[0]);

}



int main()

{

    setlocale(LC_ALL, "");



    const wchar_t* s1 = L"UNICODE转换成UTF-8";

    cout << WC2UT(s1).c_str() << endl;



    const char* s2 = "ANSI转换成UNICODE";

    wcout << MB2WC(s2).c_str() << endl;



    const wchar_t* s3 = L"UNICODE转换成ANSI";

    cout << WC2MB(s3).c_str() << endl;



    return 0;

}

------解决方案--------------------
楼上的对哦,我也在用住。
这种格式转换最蛋痛了
------解决方案--------------------
可以按一楼mvp级会员的做啊

另外二楼的头像比较风骚,也比较蛋疼