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 @   一个野指针  阅读(154)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示