【转】C++ int,char,string,CString类型转换(整理总结)

上一篇 / 下一篇  2012-08-22 10:19:10 / 个人分类:开发知识

 
51Testing软件测试网yE^ocs

#include <string>//使用C++标准库的string类时

1p-h)D/Q)i2z#F051Testing软件测试网)kCM[UZ!me'P9n

using namespace std;//同上51Testing软件测试网/Z!Y(HAk_"d z5k

51Testing软件测试网;x'\5kRD1OO{

#include <sstream>51Testing软件测试网GjY7} Vl*|1]

51Testing软件测试网)|4CDDv(n:jE

#include <iostream>

lZ8R"vHqE051Testing软件测试网u e]5\hI

#include <stdlib.h>//要将string类和int类型直接转换最好有这些包含,51Testing软件测试网JOZ!BT,Zu Q D2qc

+V g0A3X1I?0//因为自己写一个转换函数比较方便,函数定义参考如下

1A!~ei{ s+u5l051Testing软件测试网iw7C.q1Ot

string getstring ( const int n )

9`QipHz)LN0

vmw,P1`$y0?0{51Testing软件测试网w_5f!ge(CA(|

pzix n-HZ0std::stringstream newstr;51Testing软件测试网)H;R'w*HM\2hrT
newstr<<n;
^&n6x wq$X1Xcl0return newstr.str();
51Testing软件测试网,r5v i)dq*\

51Testing软件测试网?%lIsB:o1X

}

&gB#pe5d a+mK!]051Testing软件测试网_-_ERr5\s

51Testing软件测试网v6Vu? Q7t

51Testing软件测试网'xb#u th9[ s

string 转 CString51Testing软件测试网%_rQ-C0^A
CString.format(”%s”, string.c_str());

\jRR0P(Lcm?0

d l8T],}0char 转 CString51Testing软件测试网GaK WV Z1oG'u;Z
CString.format(”%s”, char*);51Testing软件测试网hE];{9M_m!Z

51Testing软件测试网_:]9x k;x ?pd

char 转 string
+hJIi1^d0string s(char *);51Testing软件测试网 ~*P;I7q3u lV

UO^}[5?0string 转 char *
2q W kdp$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());

QQ8xQ F-e L%~Ay6IG051Testing软件测试网&pl` A Y8U^Es

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'P Er%a2]
3,copy()

t(DWVN'V0

T_1|Lm5\.O$D6|z)Ps0—————————————————————

M"~`EU5wU0

BR:W*|x)S)Hp"bQK0CString与int、char*、char[100]之间的转换- -51Testing软件测试网/|:@!zv Tpo

51Testing软件测试网K7F(b-Z wG@&c

CString与int、char*、char[100]之间的转换- -51Testing软件测试网ekgm~-_)?e

51Testing软件测试网 }Hk8d&d6l@^Q

CString互转int51Testing软件测试网"c'}o ZC3E R

51Testing软件测试网WNE"Pl,[&XH

将字符转换为整数,可以使用atoi、_atoi64或atol。51Testing软件测试网DMPWZU:_w
而将数字转换为CString变量,可以使用CString的Format函数。如
X$Y'l(M|o@4F}(p0CString s;51Testing软件测试网(A0\]$Q z+c Ol
int i = 64;
.aM@/aT0s.Format(”%d”, i)
Pq2E!Z$[ h'G0g5Iw0Format函数的功能很强,值得你研究一下。51Testing软件测试网x"B'[2Bn^'Q

2WG6]KgQ0void CStrDlg::OnButton1()51Testing软件测试网{'N"J!MC4x9kShUb
{
h)l4a%o(j5U0// TODO: Add your control notification handler code here
3B%zj Q,B@]W,B0CString
+~#FJ/bB%b \-G'o0ss=”1212.12″;
&RzJqR(u0int temp=atoi(ss);51Testing软件测试网\8[dYwM
CString aa;
%_S`^8P0aa.Format(”%d”,temp);
/PlV c!D/[ r e0AfxMessageBox(”var is ” + aa);
-` ]L,`gG9C$W0}51Testing软件测试网g/B9WQF K

NknM5Y+}0sart.Format(”%s”,buf);

cN,e(R0Nr @051Testing软件测试网4~.[#K~ev`

CString互转char*

-L3WD\*@b SX:M{W$G051Testing软件测试网n_iPj%]"t

///char * TO cstring
Djpp2| ChE!tc0CString strtest;51Testing软件测试网!@ g$f+Oo O6]
char * charpoint;51Testing软件测试网gvbA;?Ij;V
charpoint=”give string a value”;51Testing软件测试网,_ l:a:U@0a
strtest=charpoint;

c$M-zbN_X1iLL0

|1Xp5a*z0///cstring TO char *51Testing软件测试网6arL'D?*R
charpoint=strtest.GetBuffer(strtest.GetLength());

Zi+zU^051Testing软件测试网[Tgj-v/Lq

标准C里没有string,char *==char []==string

!s.M Mi(f)M051Testing软件测试网"rI\5^c)o

可以用CString.Format(”%s”,char *)这个方法来将char *转成CString。要把CString转成char *,用操作符(LPCSTR)CString就可以了。51Testing软件测试网.r0a] @6S X x

51Testing软件测试网/ZYN O.[(U

CString转换 char[100]

D$?!rk%Y+e6a k_0

:Qa [%?m6j;T{g/d0char a[100];51Testing软件测试网!v5aHR2S B_
CString str(”aaaaaa”);
i%M't)~0P6_0strncpy(a,(LPCTSTR)str,sizeof(a));51Testing软件测试网}CK-F0P.V#N@E


TAG:

 

评分:0

我来说两句

Open Toolbar