首页 > 技术文章 > w2c

liuhan333 2017-04-10 23:05 原文

#include <windows.h>
#include <string>

bool WcharToChar(const wchar_t* wp, std::string& str)
{
    int len = WideCharToMultiByte(CP_ACP, 0, wp, wcslen(wp), NULL, 0, NULL, NULL);
    str.resize(len + 1);
    return WideCharToMultiByte(CP_ACP, 0, wp, wcslen(wp), (char*)str.data(), len, NULL, NULL) > 0;
}

bool CharToWchar(const char* c, std::wstring& wstr)
{
    int len = MultiByteToWideChar(CP_ACP, 0, c, strlen(c), NULL, 0);
    wstr.resize(len + 1);
    return MultiByteToWideChar(CP_ACP, 0, c, strlen(c), (wchar_t*)wstr.data(), len) > 0;
}

 

推荐阅读