码迷,mamicode.com
首页 > 其他好文 > 详细

std::wstring std::string w2m m2w

时间:2019-07-05 22:28:14      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:dep   unsigned   static   turn   resize   text   acp   als   class   

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

标签:dep   unsigned   static   turn   resize   text   acp   als   class   

原文地址:https://www.cnblogs.com/herd/p/11140907.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!