1. 程式人生 > >const wchar_t*轉換成string型別

const wchar_t*轉換成string型別

直接上程式碼:

std::string CWTOA(const wchar_t* lpwcszWString)
{
 char* pElementText;//定義一個char型別指標
 int iTextLen;//定義長度
 
 iTextLen = ::WideCharToMultiByte(CP_ACP, 0, lpwcszWString, -1, NULL, 0, NULL, NULL);//獲取傳入字串長度


 pElementText = new char[iTextLen + 1];//開闢空間
 memset((void*)pElementText, 0, (iTextLen + 1) * sizeof(char));//清空


 ::WideCharToMultiByte(CP_ACP, 0, lpwcszWString, -1, pElementText, iTextLen, NULL, NULL);//轉換
 std::string strReturn(pElementText);//定義string型別變數,以構造的方式傳入字串賦值
 delete[] pElementText;
return strReturn;//返回你懂的;