您的位置:首页 > 其它

字符串和数字之间的转换

2014-02-25 19:50 316 查看
数字转换为字符串

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

[html] view
plaincopyprint?

# include <stdio. h>  

# include <stdlib. h>   

void main (void)  

{  

    int num = 100;  

    char str[25];  

    itoa(num, str, 10);  

    printf("The number 'num' is %d and the string 'str' is %s. \n" ,  

                       num, str);  

}  

//itoa()函数有3个参数:第一个参数是要转换的数字,第二个参数是要写入转换结果的目标字符串,第三个参数是转移数字时所用的基数。在上例中,转换基数为10。  

 

    itoa()                将整型值转换为字符串

    ltoa()                将长整型值转换为字符串

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

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

[html] view
plaincopyprint?

int H, M, S;  

    string time_str;  

    H=seconds/3600;  

    M=(seconds%3600)/60;  

    S=(seconds%3600)%60;  

    char ctime[10];  

    sprintf(ctime, "%d:%d:%d", H, M, S);             // 将整数转换成字符串  

    time_str=ctime;                                                 // 结果   

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

[html] view
plaincopyprint?

# include <stdio. h>  

# include <stdlib. h>  

  

void main (void)  

{  

    double num = 12345.678;  

    char * sir;  

    int dec_pl, sign, ndigits = 3; /* Keep 3 digits of precision. * /  

    str = fcvt(num, ndigits, &dec-pl, &sign); /* Convert the float  

                                                 to a string. * /  

    printf("Original number; %f\n" , num) ;  /* Print the original  

                                                 floating-point  

                                                    value. * /  

    printf ("Converted string; %s\n",str);    /* Print the converted  

                                                string's value. * /  

    printf ("Decimal place: %d\n" , dec-pi) ; /* Print the location of  

                                                 the decimal point. * /  

    printf ("Sign: %d\n" , sign) ;            /* Print the sign.  

                                                 0 = positive,  

                                                 1 = negative. * /  

}  

 

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

[html] view
plaincopyprint?

#include <sstream>  

#Include <string>  

string num2str(double i)  

{  

        stringstream ss;  

        ss<<i;  

        return ss.str();  

}  

 

 

将字符串转换为数字

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

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

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

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

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

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

[html] view
plaincopyprint?

#include<stdio.h>  

   

void main()  

   

{  

   

  char *a="-100.23";  

   

  char *b="200e-2";  

   

  float c;  

   

  c=atof(a)+atof(b);  

   

  printf("c=%.2f\n",c);  

   

}  

 

(2)用sscanf函数

[html] view
plaincopyprint?

char    str[] = "15.455";  

   int     i;  

   float     fp;  

   sscanf( str, "%d", &i );         // 将字符串转换成整数   i = 15  

   sscanf( str, "%f", &fp );      // 将字符串转换成浮点数 fp = 15.455000  

   //打印  

   printf( "Integer: = %d ",  i+1 );  

   printf( "Real: = %f ",  fp+1 );   

   return 0;  

 

(3)stringsteam

[html] view
plaincopyprint?

int str2num(string s)  

 {     

        int num;  

        stringstream ss(s);  

        ss>>num;  

        return num;  

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: