有妖气 发表于 2016-12-10 16:44:03

自己用的 C++ UTF8转GBK 源代

stringUTF8ToGB2132(stringstrSrc)
{
        string result;
        WCHAR *wstrSrc = NULL;
        char *szRes = NULL;
        int i;
        // UTF8转换成Unicode   
        i = MultiByteToWideChar(CP_UTF8, 0, strSrc.c_str(), -1, NULL, 0);
        wstrSrc = new WCHAR;
        MultiByteToWideChar(CP_UTF8, 0, strSrc.c_str(), -1, wstrSrc, i);
        // Unicode转换成GB2312   
        i = WideCharToMultiByte(CP_ACP, 0, wstrSrc, -1, NULL, 0, NULL, NULL);
        szRes = new char;
        WideCharToMultiByte(CP_ACP, 0, wstrSrc, -1, szRes, i, NULL, NULL);
        result = string(szRes);
        if (wstrSrc != NULL)
        {
                delete[]wstrSrc;
                wstrSrc = NULL;
        }
        if (szRes != NULL)
        {
                delete[]szRes;
                szRes = NULL;
        }
        return result;
}

喜欢C++ win32的一起交流啊

有妖气 发表于 2016-12-10 16:44:22

代码是不是很简单 很清爽, 目前项目再用挺不错的

qxuem 发表于 2017-1-16 12:37:20

楼主咋都不发了
页: [1]
查看完整版本: 自己用的 C++ UTF8转GBK 源代