VC++ USC2到Ansi编码

自己在用C++做网页访问时出现了 \u9ad8\u8003 的字符串,所以Mark下来,方便以后使用

std::string usc2ToAnsi(std::string str)
{
    str = replaceAll(str, "\\u", "0X");
    std::string::size_type pos(0);
    while (std::string::npos != (pos = str.find("0X", pos)))
    {
        std::string tmpString = str.substr(pos, 6);
        WCHAR wcChinese[2];
        wcChinese[0] = (WCHAR)strtol(tmpString.c_str(), NULL, 16);
        wcChinese[1] = 0;
        char Buffer[3];
        ZeroMemory(Buffer, sizeof(Buffer));
        WideCharToMultiByte(CP_ACP, 0, wcChinese, -1, Buffer, sizeof(Buffer), NULL, NULL);
//      std::cout << Buffer << std::endl;
        str.replace(pos, 6, Buffer);
        pos += strlen(Buffer);
    }
    return str;
}

你可能感兴趣的:(VC++,MFC)