首先解釋下三者的含義
CString 是一種很有用的數(shù)據(jù)類型。它們很大程度上簡化了MFC中的許多操作(適用于MFC框架),使得MFC在做字符串操作的時候方便了很多。需要包含頭文件#include <afx.h>
C++是字符串,功能比較強(qiáng)大。要想使用標(biāo)準(zhǔn)C++中string類,必須要包含#include <string>// 注意是<string>,不是<string.h>,帶.h的是C語言中的頭文件。Char * 專門用于指以'/0'為結(jié)束的字符串.
以下方法來進(jìn)行轉(zhuǎn)換:
// CharConvert.cpp : 定義控制臺應(yīng)用程序的入口點(diǎn)。//#include "stdafx.h"#include<iostream>#include<afx.h>#include<string>using namespace std;int _tmain(int argc, _TCHAR* argv[]){	//此方法適用于“多字節(jié)” 否則會出現(xiàn)'strcpy' : cannot convert parameter 2 from 'unsigned short *' to 'const char *'	/*CString str("Hello zzu");	cout<<"the CString is "<<str<<endl;	char a[100];	strcpy(a,str);	cout<<"the convert to char * is "<<a<<"(char *)"<<endl;*/	//CString 轉(zhuǎn)換成string	//  CString c_str1("Hello Zhengzhou University");	//string str;	//str=c_str1.GetBuffer(0);	//c_str1.ReleaseBuffer();  //否則就沒有釋放緩沖區(qū)所占的空間	//cout<<str<<endl;	//cout<<"/n"<<c_str1<<endl;	//string 轉(zhuǎn)換成CString	/*string str1("Hello College of Information Engineering");	CString c_str2;	c_str2=str1.c_str(); //c_str():生成一個const char*指針,指向以空字符終止的數(shù)組。	cout<<"the CString is "<<c_str2<<endl;	cout<<"the string is "<<str1<<endl;*/	//string轉(zhuǎn)換成const char*	//方法一:	//string str2("Hello College of Information Engineering");	//const char * str3;  //常數(shù)指針	//str3=str2.c_str();	//cout<<"string  is  "<<str2<<endl;	//cout<<"const char is "<<str3<<endl;		//方法二: // /* string str("Hello College of Information Engineering");	//const char * c_str;	//c_str=str.data();	//cout<<"string is  "<<str<<endl;	//cout<<"const char is"<<c_str<<endl;*/				//string 直接轉(zhuǎn)換成char*	///*string s1 = "abcdefg";	//char *data;	//int len = s1.length();	//data = (char *)malloc((len)*sizeof(char));	//s1.copy(data,len,0);	//cout<<len<<endl;	//cout<<data<<endl;*/	      	//string.copy()的用法	  //size_t length;  //  char buffer[8];  // string str("Test string......");  //    // length=str.copy(buffer,7,6);    //從buffer6,往后數(shù)7個,相當(dāng)于[ buffer[6], buffer[6+7] )  // buffer[length]='/0';            //加上'/0'使得buffer就到buffer[length]為止;   // cout <<"buffer contains: " << buffer <<endl;  //char * 轉(zhuǎn)換成string	//char *到string  /* char * c_str="zzu";	string str(c_str);	cout<<"the c_str "<<c_str<<endl;	cout<<"the string is"<<str<<"and length is "<<str.length()<<endl;*/	//char a[]="asd";	//cout<<strlen(a)<<endl; //為什么顯示的是3,不是有一個/0嗎	//char * 到CString  	////char * c_str1="zzu";	////CString str; //可以直接轉(zhuǎn)換,因?yàn)镃String存在重載(在多字節(jié)字符集下適用)	////str.Format("%s",c_str1);	////cout<<"the c_str1 is"<<c_str1<<endl;		////cout<<"the CString is"<<str<<endl;	//方法1:使用API:WideCharToMultiByte進(jìn)行轉(zhuǎn)換(使用過,有效)   //CString str= CString("This is an example!");   //int n = str.GetLength(); //按字符計(jì)算,str的長度   //int len = WideCharToMultiByte(CP_ACP,0,str,n,NULL,0,NULL,NULL);//按Byte計(jì)算str長度   //char *pChStr = new char[len+1];//按字節(jié)為單位   //WideCharToMultiByte(CP_ACP,0,str,n,pChStr,len,NULL,NULL);//寬字節(jié)轉(zhuǎn)換為多字節(jié)編碼   // pChStr[len] = '/0';//不要忽略末尾結(jié)束標(biāo)志   //用完了記得delete []pChStr,防止內(nèi)存泄露	return 0;}同時需要注意的是,我們在平成寫程序時,最好搞清我們的編譯環(huán)境中的編碼方式,不同的編碼方式可以會導(dǎo)致一些字符轉(zhuǎn)換失敗,當(dāng)我們編程開始,就要想好在哪個編碼方式下進(jìn)行,以免最后出現(xiàn)換編碼方式了,代碼卻出現(xiàn)很多錯誤(很讓人頭疼),比如sqlite數(shù)據(jù)庫中的中文字符亂碼問題就是由于編碼方式和數(shù)據(jù)庫中默認(rèn)的編碼方式不一致。這點(diǎn)在我們寫程序時一定謹(jǐn)記。
MFC中char*,string和CString之間的轉(zhuǎn)換補(bǔ)充
一、 將CString類轉(zhuǎn)換成char*(LPSTR)類型
方法一,使用強(qiáng)制轉(zhuǎn)換。例如:
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);
方法三,使用CString::GetBuffer。例如:
CString s(_T("This is a  test ")); 
LPTSTR p = s.GetBuffer(); 
// 在這里添加使用p的代碼 
if(p != NULL) *p =  _T('/0'); 
s.ReleaseBuffer(); 
// 使用完后及時釋放,以便能使用其它的CString成員函數(shù)
CString str = "ABCDEF"; 
char *pBuf = str,GetBuffer( 0 ); 
str.ReleaseBuffer();
二、 string轉(zhuǎn)char*
string 是c++標(biāo)準(zhǔn)庫里面其中一個,封裝了對字符串的操作
把string轉(zhuǎn)換為char* 有3種方法:
1。data(),返回沒有”/0“的字符串?dāng)?shù)組 
如:
string str="abc";
char  *p=str.data();
2.c_str 返回有”/0“的字符串?dāng)?shù)組 
如:string  str="gdfd";
    char *p=str.c_str();
3 copy
比如
string  str="hello";
char p[40];
str.copy(p,5,0); //這里5,代表復(fù)制幾個字符,0代表復(fù)制的位置
*(p+5)='/0';  //要手動加上結(jié)束符
cout < < p;
三、 字符串string轉(zhuǎn)換為其它數(shù)據(jù)類型
temp="123456";
1)短整型(int)
i =  atoi(temp);
2)長整型(long)
l =  atol(temp);
3)浮點(diǎn)(double)
d =  atof(temp);
string s; d= atof(s.c_str());
4)BSTR變量
BSTR bstrValue =  ::SysAllocString(L"程序員");
...///完成對bstrValue的使用
SysFreeString(bstrValue);
5)CComBSTR變量
CComBSTR類型變量可以直接賦值
CComBSTR  bstrVar1("test");
CComBSTR bstrVar2(temp);
6)_bstr_t變量
_bstr_t類型的變量可以直接賦值
_bstr_t  bstrVar1("test");
_bstr_t bstrVar2(temp);
四、 Char*轉(zhuǎn)換為string
如果要把一個char 轉(zhuǎn)換成string, 可以使用 string s(char *);
五、string 轉(zhuǎn)CString
CString.format("%s", string.c_str());
六、char 轉(zhuǎn)CString
CString.format("%s", char*);
七、 CString -> string
string  s(CString.GetBuffer());  
GetBuffer()后一定要ReleaseBuffer(),否則就沒有釋放緩沖區(qū)所占的空間.
八、CString互轉(zhuǎn)int
將字符轉(zhuǎn)換為整數(shù),可以使用atoi、_atoi64或atol。  
而將數(shù)字轉(zhuǎn)換為CString變量,可以使用CString的Format函數(shù)。如  
CString s; int i = 64; s.Format("%d", i)新聞熱點(diǎn)
疑難解答