std::wstring std::string w2m m2w

Posted herd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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));//转换编码

以上是关于std::wstring std::string w2m m2w的主要内容,如果未能解决你的问题,请参考以下文章

vc++中std::string/std::wstring的宏是啥?

如何将 UTF-8 std::string 转换为 UTF-16 std::wstring?

将 std::string 转换为 std::wstring 时,C++17 codecvt 抛出“错误转换”

BSTR 到 std::string (std::wstring) 反之亦然

cpp中std::string和std::wstring 相互转换

对std::string和std::wstring区别的解释,807个赞同,有例子