【转】C++ int,char,string,CString类型转换(整理总结)
上一篇 / 下一篇 2012-08-22 10:19:10 / 个人分类:开发知识
#include <string>
using namespace std;
#include <sstream>
#include <iostream>
lZ8R"vHqE051Testing软件测试网ue ]5\hI#include <stdlib.h>
+Vg0A3X1I?0
string getstring ( const int n )
9`QipHz)LN0vmw,P1`$y0?0{51Testing软件测试网w_5f!ge(CA(|
pzixn-HZ0
^&n6x
wq$X1Xcl0
}
&gB#pe5d a+mK!]051Testing软件测试网_-_ERr5\sstring 转 CString51Testing软件测试网%_rQ-C0^A
CString.format(”%s”, string.c_str());
d
l8T],}0char 转 CString51Testing软件测试网GaK
WVZ1oG'u;Z
CString.format(”%s”, char*);51Testing软件测试网hE];{9M_m!Z
char 转 string
+hJIi1^d0string s(char *);51Testing软件测试网~*P;I7q3ulV
UO^}[5?0string 转 char *
2q
Wkdp$U:WX0char *p = string.c_str();51Testing软件测试网;gj[oSD&F
&TJ*L*v f;L&y(t0CString 转 string
!Q/IvK#Hm-L0string s(CString.GetBuffer());
1,string -> CString
/s4K1q:xF0CString.format(”%s”,
string.c_str());51Testing软件测试网/WGlp\ydSz.z
I
用c_str()确实比data()要好.51Testing软件测试网5vVn@#R4g[l
|
2,char -> string
s3RwX"V0string s(char
*);
6tZ$n5s `L$t0只能初始化,在不是初始化的地方最好还是用assign().
E-}'l#i;q03,CString -> string
vwSw?6u
z8o s~0string
s(CString.GetBuffer());
1Xj:w9z;d(?_0tS0GetBuffer()后一定要ReleaseBuffer(),否则就没有释放缓冲区所占的空间.51Testing软件测试网NrNWDx7j
9V-r#e"OUs2_0qs0《C++标准函数库》中说的
5|@UGg#S;F)d(cj0有三个函数可以将字符串的内容转换为字符数组和C—string
&S*K^;}?#\n
M'X01.data(),返回没有”\0“的字符串数组
n}}&}
tQ02,c_str(),返回有”\0“的字符串数组51Testing软件测试网+|g'PEr%a2]
3,copy()
T_1|Lm5\.O$D6|z)Ps0—————————————————————
M"~`EU5wU0BR:W*|x)S)Hp"bQK0CString与int、char*、char[100]之间的转换- -51Testing软件测试网/|:@!zv Tpo
51Testing软件测试网K7F(b-ZwG@&cCString与int、char*、char[100]之间的转换- -51Testing软件测试网ekgm~-_)?e
51Testing软件测试网 }Hk8d&d6l