天天看點

CString 轉換成 char *

寫程式時 要實作CString轉換成 char *,晚上找了一些方法,可總是會有錯誤,不是我想要的。

比如下面這兩種轉換格式:

1.

<span style="white-space:pre">	</span>CString strItem("1238djj");
	char *chItem1;
	chItem1 = (LPSTR)(LPCTSTR)strItem1;
      

2.

<span style="white-space:pre">	</span> CString strItem("1238djj");
	 char chItem1[100];
	strncpy(chItem1,(LPSTR)(LPCTSTR)strItem1,sizeof(chItem1));
      

其中的 chItem1的值 隻是字元串strItem1的首字母。

正确的方法:

<span style="white-space:pre">	</span>wchar_t *chItem1,*chItem2;
	chItem1=(LPWSTR)strItem1.GetBuffer(strItem1.GetLength());
	chItem2=(LPWSTR)strItem2.GetBuffer(strItem2.GetLength());      

這樣才可以得到字元串的全部字元。

//wchar_t *轉換成int
int intItem1 = _ttol(chItem1);      

版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。