std::wstring std::string w2m m2w

static std::wstring m2w(std::string ch, unsigned int CodePage = CP_ACP)
{
    if (ch.empty())return L"";
    std::wstring ret;
    DWORD dwOutSize = 0;
    dwOutSize = MultiByteToWideChar(CodePage, 0, ch.c_str(), -1, NULL, 0);

    ret.resize(dwOutSize - 1);
    MultiByteToWideChar(CodePage, 0, ch.c_str(), ch.size(), &ret[0], dwOutSize);

    return ret;
}

 

static std::string w2m(std::wstring wch, unsigned int CodePage = CP_ACP)
{
    std::string ret;
    DWORD dwOutSize = 0;
    dwOutSize = WideCharToMultiByte(CodePage, 0, wch.c_str(), -1, NULL, 0, NULL, FALSE);

    char *pwText = 0;
    pwText = new char[dwOutSize];
    pwText[dwOutSize - 1] = '\0';

    WideCharToMultiByte(CodePage, 0, wch.c_str(), wch.size(), pwText, dwOutSize, NULL, FALSE);

    ret = pwText;
    if (pwText)delete[]pwText;

    return ret;
}

 

std::string name = w2m(m2w(obj->GetName(), CP_UTF8));//转换编码

posted @ 2019-07-05 21:39  西北逍遥  阅读(545)  评论(0编辑  收藏  举报