1. 程式人生 > >const char*轉換成wstring型別

const char*轉換成wstring型別

轉載自:https://blog.csdn.net/cqltbe131421/article/details/52597813

直接上程式碼:

 

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;
}