char*轉換成CString
若將char*轉換成CString,除了直接賦值外,還可使用CString::format進行。例如:
char chArray[] = "This is a test";
char * p = "This is a test";
或
LPSTR p = "This is a test"; 或在已定義Unicode應的用程序中
TCHAR * p = _T("This is a test"); 或
LPTSTR p = _T("This is a test");
CString theString = chArray;
theString.format(_T("%s"), chArray);
theString = p;
2、CString轉換成char*
若將CString類轉換成char*(LPSTR)類型,常常使用下列三種方法:
方法一,使用強制轉換。例如:
CString theString( "This is a test" );
LPTSTR lpsz =(LPTSTR)(LPCTSTR)theString; 方法二,使用strcpy。例如:
CString theString( "This is a test" );
LPTSTR lpsz = new TCHAR[theString.GetLength()+1];
_tcscpy(lpsz, theString); 需要說明的是,strcpy(或可移值Unicode/MBCS的_tcscpy)的第二個參數是 const wchar_t* (Unicode)或const char* (ANSI),系統編譯器將會自動對其進行轉換。
方法三,使用CString::GetBuffer。例如:
CString s(_T("This is a test "));
LPTSTR p = s.GetBuffer();
// 在這里添加使用p的代碼
if(p != NULL) *p = _T('\0');
s.ReleaseBuffer(); // 使用完后及時釋放,以便能使用其它的CString成員函數
posted on 2007-08-03 17:05
喬棟 閱讀(352)
評論(0) 編輯 收藏 引用 所屬分類:
C++的健身房