CString类常用方法----Format(),sprintf(),itoa(),ltoa(),ultoa(),atoi(),atol(),atof()

void Format( LPCTSTR lpszFormat, ... );和printf的使方法一样

void Format( UINT nFormatID, ... );利用资源格式化字符串,这个比上面的省空间,方便改,功能一样

作用:像printf一样格式化字符串

 

int sprintf( char *buffer, const char *format [, argument] ... );//用法和 printf 一样

 

char *itoa( int value, char *string, int radix );

注:

   参数:int value :是要转为字符串的int型

          char *string :存放字符串的缓冲区

          int radix  :将int转换为多少进制的数存放在缓冲区中 

 

char *ltoa( long value, char *string, int radix );

注:

   参数:long value :是要转为字符串的long

          char *string :存放字符串的缓冲区

          int radix  :将long转换为多少进制的数存放在缓冲区中 

 

char *ultoa( unsigned long value, char *string, int radix );

注:

   参数:unsigned long value :是要转为字符串的unsigned long型

          char *string :存放字符串的缓冲区

          int radix  :将unsigned long转换为多少进制的数存放在缓冲区中 

 

int atoi( const char *string );

注:

   参数:const char *string :是要转为int型的字符串

   返回值:字符串对应的int型

long atol( const char *string );

注:

   参数:const char *string :是要转为long型的字符串

   返回值:字符串对应的long型

double atof( const char *string );

注:

   参数:const char *string :是要转为double型的字符串

   返回值:字符串对应的double型

例:

1.void Format( LPCTSTR lpszFormat, ... );

 

CString a,b;
a = "12卡拉";

b.Format("%s", a);                       // b的值为"12卡拉";,因为是把a格式化到b中,相当于a=b
b.Format("%d", a.GetLength());   // b的值为6,因为是把a的字节长格式化到b中

 


2.void Format( UINT nFormatID, ... );

(1)先打开"ResourceView"视窗

(2)点开"String Table"

(3)双击"String Table [English [U.S.]]"

(4)右键右边的下边空白,点"New String"

(5)在"Caption"右边的框中添:%d(这里也可以改成%s,%c等,根据须要来决定)

(6)把上面的"ID"记住

 

CString a,b;
a = "12卡拉";
b.Format(添上面的ID号, a.GetLength());   // b的值为6,因为是把a的字节长格式化到b中

 

例2:

 

char *p = new char[255];
 int a = 10;
 double b = 3.14;
 long c = 20;
 unsigned d = 30;
 char *e = "abcde";
 CString f;

 sprintf(p, "%d", a);   //p中的值为10
 sprintf(p, "%lf", b);  //p中的值为3.140000
 sprintf(p, "%ld", c);  //p中的值为20
 sprintf(p, "%u", d);   //p中的值为30
 sprintf(p, "%s", e);   //p中的值为abcde

 f.Format("%d", a);   //f中的值为10
 f.Format("%lf", b);  //f中的值为3.140000
 f.Format("%ld", c);  //f中的值为20
 f.Format("%u", d);   //f中的值为30
 f.Format("%s", e);   //f中的值为abcde

 itoa(a, p, 10);      //p中的值为10
 ltoa(c, p, 10);      //p中的值为20
 ultoa(d, p, 10);     //p中的值为30

 char *g = "40";
 char *h = "4.59";
 int i = atoi(g);     //i中的值为40
 long j = atol(g);    //j中的值为40
 double k = atof(h);  //k中的值为4.58999999...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值