使用强制转换。例如:
CString theString( "This is a test " ); LPTSTR lpsz =(LPTSTR)(LPCTSTR)theString; char *buf;
CString str = "hello "; buf = (LPSTR)(LPCTSTR)str; CString str = "ABC ";
char* chArr; chArr = (char*)(LPCTSTR)str; CString str( "50 ");
int nConv = atoi( str ); 使用strcpy。例如: CString theString( "This is a test " ); LPTSTR lpsz = new TCHAR[theString.GetLength()+1]; _tcscpy(lpsz, theString); char szBuff[100];
CString str = "123456abc "; strncpy( szBuff, str, strlen( str ) ); 或 strncpy(szBuff, str, str.GetLength()); //不要+1了,防越界
使用CString::GetBuffer。例如: CString s(_T( "This is a test ")); LPTSTR p = s.GetBuffer(); // 在这里添加使用p的代码 if(p != NULL) *p = _T( '\0 '); s.ReleaseBuffer(); // 使用完后及时释放,以便能使用其它的CString成员函数 CString str( "this is a test! ");
char* szBuf = str.GetBuffer( str.GetLenghth() ); //...... str.ReleaseBuffer(); |
|