字符串和数字之间的转换

数字转换为字符串

(1)C语言提供了几个标准库函数,可以将任意类型(整型、长整型、浮点型等)的数字转换为字符串。以下是用itoa()函数将整数转换为字符串的一个例子:

[html]  view plain copy print ?
  1. # include <stdio. h>  
  2. # include <stdlib. h>   
  3. void main (void)  
  4. {  
  5.     int num = 100;  
  6.     char str[25];  
  7.     itoa(num, str, 10);  
  8.     printf("The number 'num' is %d and the string 'str' is %s. \n" ,  
  9.                        num, str);  
  10. }  
  11. //itoa()函数有3个参数:第一个参数是要转换的数字,第二个参数是要写入转换结果的目标字符串,第三个参数是转移数字时所用的基数。在上例中,转换基数为10。  

 

    itoa()                将整型值转换为字符串
    ltoa()                将长整型值转换为字符串
    ultoa()               将无符号长整型值转换为字符串


(2)上述函数与ANSI标准是不兼容的。能将整数转换为字符串而且与ANSI标准兼容的方法是使用sprintf()函数,请看下例:

[html]  view plain copy print ?
  1. int H, M, S;  
  2.     string time_str;  
  3.     H=seconds/3600;  
  4.     M=(seconds%3600)/60;  
  5.     S=(seconds%3600)%60;  
  6.     char ctime[10];  
  7.     sprintf(ctime, "%d:%d:%d", H, M, S);             // 将整数转换成字符串  
  8.     time_str=ctime;                                                 // 结果   

 
在将浮点型数字转换为字符串时,需要使用另一组函数,以下是fcvt()函数将浮点型值转换为字符串的一个例子:

[html]  view plain copy print ?
  1. # include <stdio. h>  
  2. # include <stdlib. h>  
  3.   
  4. void main (void)  
  5. {  
  6.     double num = 12345.678;  
  7.     char * sir;  
  8.     int dec_pl, sign, ndigits = 3; /* Keep 3 digits of precision. * /  
  9.     str = fcvt(num, ndigits, &dec-pl, &sign); /* Convert the float  
  10.                                                  to a string. * /  
  11.     printf("Original number; %f\n" , num) ;  /* Print the original  
  12.                                                  floating-point  
  13.                                                     value. * /  
  14.     printf ("Converted string; %s\n",str);    /* Print the converted  
  15.                                                 string's value. * /  
  16.     printf ("Decimal place: %d\n" , dec-pi) ; /* Print the location of  
  17.                                                  the decimal point. * /  
  18.     printf ("Sign: %d\n" , sign) ;            /* Print the sign.  
  19.                                                  0 = positive,  
  20.                                                  1 = negative. * /  
  21. }  

 

(3)C++ stringstream也能完成这样的功能:

[html]  view plain copy print ?
  1. #include <sstream>  
  2. #Include <string>  
  3. string num2str(double i)  
  4. {  
  5.         stringstream ss;  
  6.         ss<<i;  
  7.         return ss.str();  
  8. }  

 

 

将字符串转换为数字

(1)使用C库里面提供的函数

       atoi(将字符串转换成整型数)

  atol(将字符串转换成长整型数)

  strtod(将字符串转换成浮点数)

  strtol(将字符串转换成长整型数)

  strtoul(将字符串转换成无符号长整型数)

[html]  view plain copy print ?
  1. #include<stdio.h>  
  2.    
  3. void main()  
  4.    
  5. {  
  6.    
  7.   char *a="-100.23";  
  8.    
  9.   char *b="200e-2";  
  10.    
  11.   float c;  
  12.    
  13.   c=atof(a)+atof(b);  
  14.    
  15.   printf("c=%.2f\n",c);  
  16.    
  17. }  

 

(2)用sscanf函数

[html]  view plain copy print ?
  1. char    str[] = "15.455";  
  2.    int     i;  
  3.    float     fp;  
  4.    sscanf( str, "%d", &i );         // 将字符串转换成整数   i = 15  
  5.    sscanf( str, "%f", &fp );      // 将字符串转换成浮点数 fp = 15.455000  
  6.    //打印  
  7.    printf( "Integer: = %d ",  i+1 );  
  8.    printf( "Real: = %f ",  fp+1 );   
  9.    return 0;  


 

(3)stringsteam

[html]  view plain copy print ?
  1. int str2num(string s)  
  2.  {     
  3.         int num;  
  4.         stringstream ss(s);  
  5.         ss>>num;  
  6.         return num;  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值