vc++6.0中POST发送带有中文的十六进制数据网页中文显示乱码


就像这样,中文传过去网站上显示的是 ???

百度找到说是 在中文字符 入参时进行UTF-8编码处理,下面是代码:

std::string String_TO_UTF8(std::string str)
{
    int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
    wchar_t * pwBuf = new wchar_t[nwLen + 1];
    ZeroMemory(pwBuf, nwLen * 2 + 2);

    ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen);

    int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);

    char * pBuf = new char[nLen + 1];
    ZeroMemory(pBuf, nLen + 1);

    ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);

    std::string retStr(pBuf);

    delete[]pwBuf;
    delete[]pBuf;

    pwBuf = NULL;
    pBuf = NULL;

    return retStr;
}
    //使用方法,先传入string数据,得到utf8编码处理过的string,再转为CString来使用即可
    //测试可行的,网站显示中文正常
    CString theString = "测试";
    string str;
    str = theString.GetBuffer(0);
    str = String_TO_UTF8(str);

    CString AllStr = str.c_str();

记录一下。。。

posted @ 2022-05-26 15:16  一个野指针  阅读(144)  评论(0编辑  收藏  举报