[总结]vc2008 Unicode下的CString和char* 的转换

char* UnicodeToAnsi( const wchar_t* szStr ) { int nLen = WideCharToMultiByte( CP_ACP, 0, szStr, -1, NULL, 0, NULL, NULL ); if (nLen == 0) { return NULL; } char* pResult = new char[nLen]; WideCharToMultiByte( CP_ACP, 0, szStr, -1, pResult, nLen, NULL, NULL ); return pResult; }

 

char* CStringToChar(CString str) { int nlength = str.GetLength(); int nBytes = WideCharToMultiByte(CP_ACP,0,str,nlength,NULL,0,NULL,NULL); char* ch = new char[nBytes]; memset(ch,0,nBytes+1); WideCharToMultiByte(CP_OEMCP,0,str,nlength,ch,nBytes,NULL,NULL); return ch; }

 

CString lpszFile = sPath + _T("//mywind.txt"); USES_CONVERSION; LPTSTR lpBuffer = lpszFile.GetBuffer(); char* ch = W2A(lpszFile); lpszFile.ReleaseBuffer();

wchar_t* AnsiToUnicode( const char* szStr ) { int nLen = MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, szStr, -1, NULL, 0 ); if (nLen == 0) { return NULL; } wchar_t* pResult = new wchar_t[nLen]; MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, szStr, -1, pResult, nLen ); return pResult; }

 

///////////////////////////////

ret.GetFieldValue(0,varName);  // 数据表第0列是文本格式 

m_ZoneMap[(LPCTSTR)varName.bstrVal] = packet;

你可能感兴趣的:([总结]vc2008 Unicode下的CString和char* 的转换)