http://www.cnblogs.com/luxiaoxun/archive/2012/08/03/2621803.html
1、字符串數字之間的轉換
(1)string --> char *
string str("OK");
char * p = str.c_str();
(2)char * -->string
char *p = "OK";
string str(p);
(3)char * -->CString
char *p ="OK";
CString m_Str(p);
//或者
CString m_Str;
m_Str.Format("%s",p);
(4)CString --> char *
CString str("OK");
char * p = str.GetBuffer(0);
...
str.ReleaseBuffer();
(5)string --> CString
CString.Format("%s", string.c_str());
(6)CString --> string
string s(CString.GetBuffer(0));
GetBuffer()后一定要ReleaseBuffer(),否則就沒有釋放緩沖區所占的空間,CString對象不能動態增長了。
(7)double/float->CString
double data;
CString.Format("%.2f",data); //保留2位小數
(8)CString->double
CString s="123.12";
double d=atof(s);
(9)string->double
double d=atof(s.c_str());
2、數字轉字符串:使用sprintf()函數
char str[10];
int a=1234321;
sprintf(str,"%d",a);
--------------------
char str[10];
double a=123.321;
sprintf(str,"%.3lf",a);
--------------------
char str[10];
int a=175;
sprintf(str,"%x",a);//10進制轉換成16進制,如果輸出大寫的字母是sprintf(str,"%X",a)
--------------------
char *itoa(int value, char* string, int radix);
同樣也可以將數字轉字符串,不過itoa()這個函數是平台相關的(不是標准里的),故在這里不推薦使用這個函數。
3、字符串轉數字:使用sscanf()函數
char str[]="1234321";
int a;
sscanf(str,"%d",&a);
.............
char str[]="123.321";
double a;
sscanf(str,"%lf",&a);
.............
char str[]="AF";
int a;
sscanf(str,"%x",&a); //16進制轉換成10進制
另外也可以使用atoi(),atol(),atof().
4、使用stringstream類
用ostringstream對象寫一個字符串,類似於sprintf()
ostringstream s1;
int i = 22;
s1 << "Hello " << i << endl;
string s2 = s1.str();
cout << s2;
用istringstream對象讀一個字符串,類似於sscanf()
istringstream stream1;
string string1 = "25";
stream1.str(string1);
int i;
stream1 >> i;
cout << i << endl; // displays 25
CString與string、char*的區別和轉換(轉)
http://blog.csdn.net/luoweifu/article/details/20232379
我們在C++的開發中經常會碰到string、char*以及CString,這三種都表示字符串類型,有很多相似又不同的地方,常常讓人混淆。下面詳細介紹這三者的區別、聯系和轉換:
各自的區別
char*:
char*是一個指向字符的指針,是一個內置類型。可以指向一個字符,也可以表示字符數組的首地址(首字符的地址)。我們更多的時候是用的它的第二的功能,來表示一個字符串,功能與字符串數組char ch[n]一樣,表示字符串時,最后有一個 '\0'結束符作為字符串的結束標志。
【例1】
- #include <iostream>
- using namespace std;
- void testCharArray()
- {
- char ch1[12] = "Hello Wrold"; //這里只能ch1[12],ch1[11]編譯不通過,提示array bounds overflow
- char *pch1 , *pch2 = "string";
- char *pch3, *pch4;
- pch3 = &ch1[2]; //ch1[2]的地址賦給pch3
- char ch = 'c';
- pch4 = &ch;
- pch1= ch1;
- cout << ch1 << endl; //輸出ch1[0]到\0之前的所有字符
- cout << pch1 << endl; //輸出ch1[0]到\0之前的所有字符
- cout << pch2 << endl; //輸出ch1[0]到\0之前的所有字符
- cout << pch3 << endl; //輸出ch1[2]到\0之前的所有字符
- cout << *pch3 << endl; //解引用pch3輸出pch3指向的字符
- cout << *pch4 << endl; //解引用pch4輸出pch4指向的字符
- }
結果為:
Hello Wrold
Hello Wrold
string
llo Wrold
l
C
string:
string是C++標准庫(STL)中的類型,它是定義的一個類,定義在<string>頭文件中。里面包含了對字符串的各種常用操作,它較char*的優勢是內容可以動態拓展,以及對字符串操作的方便快捷,用+號進行字符串的連接是最常用的操作。
【例2】
- #include <string>
- void testString()
- {
- string s1 = "this";
- string s2 = string(" is");
- string s3, s4;
- s3 = string(" a").append("string.");
- s4 = s1 + s2 + s3;
- cout << s1 << endl;
- cout << s2 << endl;
- cout << s3 << endl;
- cout << s4 << endl;
- cout << s4.size() << endl;
- s4.insert(s4.end()-7, 1, ' ');
- cout << s4 << endl;
- }
結果為:
this
is
astring.
this is astring.
16
this is a string.
CString
CString常用於MFC編程中,是屬於MFC的類,如從對話框中利用GetWindowText得到的字符串就是CString類型,CString定義在<afx.h>頭文件中。CString(typedef CStringT> CString)為Visual C++中最常用的字符串類,繼承自CSimpleStringT類,主要應用在MFC和ATL編程中,所以使用CString時要包含afx.h文件#include <afx.h>。
【例3】
- #include <afx.h>
- //因為CString不是標准C++庫定義的類型,沒有對<<運算符進行重載,
- //所以不能通過cout<<cstr來輸出內容,只能自己先定義一個方法。
- void printCString(const CString &cstr);
- void testCString()
- {
- char *ch = "Hello";
- string s = "Wrold";
- CString cstr1(ch), cstr2(s.c_str()), cstr3("Program");
- printCString(cstr1);
- printCString(cstr2);
- printCString(cstr3);
- CString cstr4, cstr5;
- cstr4 = cstr1 + cstr2 + cstr3;
- cstr5 = cstr1 + " " + cstr2 + " " + cstr3;
- printCString(cstr4);
- printCString(cstr5);
- }
- void printCString(const CString &cstr)
- {
- int n = cstr.GetLength();
- for(int i=0; i<n; i++)
- {
- printf("%c", cstr[i]);
- }
- printf("\n");
- }
結果為:
Hello
Wrold
Program
HelloWroldProgram
Hello Wrold Program
更多關於CString的用法請參考:http://www.cnblogs.com/Caiqinghua/archive/2009/02/16/1391190.html
使用CString時可能會遇到的一些錯誤:
編譯時會發現類似如下錯誤:
Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version. Please #define _AFXDLL or do not use /MD[d] C:\Program Files (x86)\Microsoft Visual Studio 8\VC\ce\atlmfc\include\AFX.h 24
解決方法:
(注:我用的開發環境是VS2010,其它VS的環境類似操作)
方法1:這里錯誤提示的意思是缺少_AFXDLL這個宏,因此在Project——>property中,C/C++里面Preprocessor(預編譯),加入_AFXDLL這個宏,OK搞定!!
方法2:對着你的項目點擊右鍵,依次選擇:屬性、配置屬性、常規,然后右邊有個“項目默認值”,下面有個MFC的使用,選擇“在共享 DLL 中使用 MFC”,就OK了~~~
參考文章:http://blog.csdn.net/ahjxly/article/details/8465209
http://blog.csdn.net/zhoxier/article/details/7929920
講明白了char*、string及CString的關系,可能有人對<string.h>、<cstring.h>、<string>這幾個頭文件又糊塗了,由於篇幅的原因,這部分的內容將在下一節進行說明,歡迎閱讀:《<string> 與<string.h>、<cstring>的區別》
相互的轉換
既然這三種類型都可用於表示字符串,但又是不同的類型,那他們如何轉換呢?可用的方法參見如下:
char*與string的轉換
【例4】
- void pCharToString()
- {
- //from char* to string
- char * ch = "hello world";
- string s1 = ch; //直接初始化或賦值
- string s2(ch), s3;
- s3 = string(ch);
- cout << s1 << endl;
- cout << s2 << endl;
- cout << s3 << endl;
- //from string to char*
- string str = string("string is commonly used.");
- /*************************************************************************
- 其實沒有很大的必要將string轉換成char*,因為string可以直接當成字符數組來使用,
- 即通過下標來訪問字符元素,如str[1]表示第1個字符't'
- **************************************************************************/
- const char *ch1 = str.c_str();
- cout << ch1 << endl;
- }
結果為:
hello world
hello world
hello world
string is commonly used.
char*與CString
【例5】
- void pCharToCString()
- {
- //from char* to CString
- char *ch = "char pointer.";
- CString cStr1 = ch;
- CString cStr2 = CString(ch);
- printCString(cStr1);
- printCString(cStr2);
- //from CString to char*
- CString cstr = "CString";
- char* chs=cstr.getbuffer(0);//此方法在VS2010下編譯不通過,原因見【例6】
- cout << chs << endl;
- }
結果為:
char pointer.
char pointer.
CString
string與CString
【例6】
- void stringToCString()
- {
- //from string to CString
- string s1 = "string1 to CString";
- string s2 = "string2 to CString";
- string s3 = "string3 to CString";
- CString cstr(s1.c_str());
- printCString(cstr);
- CString cstr2;
- cstr2.Format("%s", s2.c_str()); // string to CString
- printCString(cstr2);
- cstr2.Format("%s", s3.data()); // string to CString
- printCString(cstr2);
- //from CString to string
- CString cstr3 = "CString to string3";
- CString cstr4 = "CString to string4";
- string str;
- str=cstr3.GetBuffer(0);
- cout << str << endl;
- str = LPCSTR(cstr4);
- cout << str << endl;
- }
結果為:
string1 to CString
string2 to CString
string3 to CString
CString to string3
CString to string4
c_str()和data()區別是:前者返回帶'/0'的字符串,后者則返回不帶'/0'的字符串.
在VS2010環境下,cstr2.Format("%s", s2.c_str());cstr2.Format("%s", s3.data());及str=cstr3.GetBuffer(0);str = LPCSTR(cstr4); 可能會編不過,會報類似error C2664: 'void ATL::CStringT<BaseType,StringTraits>::Format(const wchar_t *,...)' : cannot convert parameter 1 from 'const char [3]' to 'const wchar_t *'的錯誤。這是因為你的工程的字符集不是多字節字符集,將你的工程屬性設置為多字節字符集即可,方法是:右鍵點擊你的工程,選擇Properties\Configurations Properties\General,在右側的Project Defaults下的Character Set選擇Use Multi-Byte Character Set。
總結
從靈活度來說,string最靈活易用,其次是CString,char*的拓展性和靈活性比較差。 一般來說在基於標准庫開發時用string,在在MFC和ATL編程時用CString。
CString、string之間的轉換還有其它的一些方向,但基本上都是通過char*作為橋梁,因為char*即可以方便地轉換成string,也可以方便地轉換成CString。
更多CString的用法也可參考以下鏈接,他們寫的更詳細,我就不再重復了。
http://www.cnblogs.com/Caiqinghua/archive/2009/02/16/1391190.html
http://blog.csdn.net/lewutian/article/details/6787024