2012年1月9日 星期一

[C++] wstring to string

Here is an example for wstring to string transform.
HRESULT DSU_WcsToStr(wstring wstrSrc, string* strDst, UINT nCodepage)
{
 _DSU_ChkPtrRtn(strDst);

 int iStrLen = wstrSrc.length();
 int nCount = WideCharToMultiByte(nCodepage, 0, wstrSrc.c_str(), iStrLen, NULL, 0, NULL, FALSE);

 char* pCHAR = new char[nCount + 1]; 
 nCount = WideCharToMultiByte(nCodepage, 0, wstrSrc.c_str(), iStrLen, pCHAR, nCount + 1, NULL, FALSE);

 pCHAR[nCount] = NULL;
 *strDst = pCHAR ;
 delete [] pCHAR ; 

 return S_OK ;
}

沒有留言:

張貼留言