WinRT(C++/CX) UTF8类型转换为std::string和Platform::String^的Unicode字符串

版权声明:本文由 修电脑的裁缝酱 原创,博客链接为 http://blog.csdn.net/huihut ,转载请注明出处。 https://blog.csdn.net/huihut/article/details/81984271

Gist 仓库地址:https://gist.github.com/huihut/8f75e2332e05673ff7e1248ad5e85339

#include <string>
#include <Windows.h>

std::string UTF8_To_Std_Str(const std::string & str)
{
    int nwLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);

    wchar_t* pwBuf = new wchar_t[nwLen + 1];
    memset(pwBuf, 0, nwLen * 2 + 2);

    MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), pwBuf, nwLen);

    int nLen = WideCharToMultiByte(CP_ACP, 0, pwBuf, -1, NULL, NULL, NULL, NULL);

    char* pBuf = new char[nLen + 1];
    memset(pBuf, 0, nLen + 1);

    WideCharToMultiByte(CP_ACP, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);

    std::string retStr = pBuf;

    delete[] pBuf;
    delete[] pwBuf;

    pBuf = NULL;
    pwBuf = NULL;

    return retStr;
}

Platform::String^ UTF8_To_Managed_Str(const std::string & str)
{
    int nwLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);

    wchar_t* pwBuf = new wchar_t[nwLen + 1];
    memset(pwBuf, 0, nwLen * 2 + 2);

    MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), pwBuf, nwLen);

    Platform::String^ pStr = ref new Platform::String(pwBuf);

    delete[] pwBuf;
    pwBuf = NULL;

    return pStr;
}
阅读更多

没有更多推荐了,返回首页