C++中CString string char* char 之間的字符轉(zhuǎn)換(多種方法)
首先解釋下三者的含義
CString 是一種很有用的數(shù)據(jù)類型。它們很大程度上簡化了MFC中的許多操作(適用于MFC框架),使得MFC在做字符串操作的時(shí)候方便了很多。需要包含頭文件#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 : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。 // #include "stdafx.h" #include<iostream> #include<afx.h> #include<string> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { //此方法適用于“多字節(jié)” 否則會(huì)出現(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():生成一個(gè)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個(gè),相當(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,不是有一個(gè)\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; }
同時(shí)需要注意的是,我們?cè)谄匠蓪懗绦驎r(shí),最好搞清我們的編譯環(huán)境中的編碼方式,不同的編碼方式可以會(huì)導(dǎo)致一些字符轉(zhuǎn)換失敗,當(dāng)我們編程開始,就要想好在哪個(gè)編碼方式下進(jìn)行,以免最后出現(xiàn)換編碼方式了,代碼卻出現(xiàn)很多錯(cuò)誤(很讓人頭疼),比如sqlite數(shù)據(jù)庫中的中文字符亂碼問題就是由于編碼方式和數(shù)據(jù)庫中默認(rèn)的編碼方式不一致。這點(diǎn)在我們寫程序時(shí)一定謹(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();
// 使用完后及時(shí)釋放,以便能使用其它的CString成員函數(shù)
CString str = "ABCDEF";
char *pBuf = str,GetBuffer( 0 );
str.ReleaseBuffer();
二、 string轉(zhuǎn)char*
string 是c++標(biāo)準(zhǔn)庫里面其中一個(gè),封裝了對(duì)字符串的操作
把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ù)制幾個(gè)字符,0代表復(fù)制的位置
*(p+5)='\0'; //要手動(dòng)加上結(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"程序員");
...///完成對(duì)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
如果要把一個(gè)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)
欄 目:C語言
下一篇:C++中友元的實(shí)例詳解
本文標(biāo)題:C++中CString string char* char 之間的字符轉(zhuǎn)換(多種方法)
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/1182.html
您可能感興趣的文章
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)數(shù)怎么表達(dá)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10深入Main函數(shù)中的參數(shù)argc,argv的使用詳解
- 01-10APUE筆記之:進(jìn)程環(huán)境詳解
- 01-10c++中inline的用法分析
- 01-10如何尋找數(shù)組中的第二大數(shù)


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置