const char*转换成wstring类型

124 阅读1分钟



直接上代码:

\

std::wstring CATOW(const char* lpcszString)//返回值类型是wstring类型
{
int unicodeLen = ::MultiByteToWideChar(CP_ACP, 0, lpcszString, -1, NULL, 0);//获取字符串长度


wchar_t* pUnicode = new wchar_t[unicodeLen + 1];//开辟宽字节内存
memset(pUnicode, 0, (unicodeLen + 1) * sizeof(wchar_t));//清空


::MultiByteToWideChar(CP_ACP, 0, lpcszString, -1, (LPWSTR)pUnicode, unicodeLen);//转换
std::wstring wString = (wchar_t*)pUnicode;//强转后赋值给返回变量
delete[] pUnicode;
return wString;
}