C++字符串和基本类型转换的各种方法总结

一、概述

1、C++中提供了字符串到基本类型转换的函数,在Windows下也提供了对应的基本类型到字符串转换的函数(在Linux没有,所以需要跨平台时不能使用)。
2、字符串到基本类型转换的接口包括atoi(字符串转int),atol(字符串转long),_atoi64(字符串转long long,注意前面有下滑线),atof(字符串转double);非数字字符串转换时,不会抛异常或奔溃。
3、Windows基本类型转换到字符串可以用itoa(int转字符串),ltoa(long转字符串),_i64toa(long long转字符串),没有ftoa函数,转换的时候要保证传入的字符数组空间足够,否则不会报错,但会写坏其他的内存值。
4、可以用sprintf和stringstream来做基本类型转换到字符串,这个是跨平台的。
5、可以用boost的lexical_cast来做各种类型间转换,包括基本类型之间。

二、字符串到基本类型转换注意点

字符串到基本类型转换时,如果字符串不符合转换到的类型格式,也能进行转换。
以atoi为例,规则如下:
 1、如果字符串是一个整数,那么正确转换。
 2、如果包含不能非数字,去掉非数字字符后面的所有字符。
 3、如果字符是一个浮点数,则丢弃小数部分。
 4、如果大于最大值,转换成最大值,如果小于最小值,转换成最小值。
下面是一个测试样例。
void test()
{
    int a = atoi("12");
    int b = atoi("32.2");
    int c = atoi("234232423423423");
    int d = atoi("df342sdfsf");
    int e = atoi("3422342ddd24");
    printf("%d %d %d %d %d",a,b,c,d,e);
}

输出结果如下:

12 32 2147483647 0 3422342

三、Windows基本类型转换到字符串

Windows基本类型转换到字符串时,如果传入的字符数组空间不足,不会截断,这会导致写坏不该写的内存空间,所以是比较危险的。
下面是一个测试样例。
void test()
{
    int a1 = 1000000000;
    char a0Str[1];
    char a1Str[1];
    a0Str[0] = ',';
    printf("%c\n",a0Str[0]);
    itoa(a1,a1Str,2);   //2表示转换成2进制,这里可以填任意进制
    printf("%c\n",a0Str[0]);
    printf("%s\n",a1Str);
}
输出结果如下:
,
1
111011100110101100101000000000

四、用sprintf将基本类型转成字符串

sprintf对基本类型转换到字符串时,如果传入的字符数组空间不足,不会截断,这会导致写坏不该写的内存空间,所以需要保证分配足够的数组空间。
void test()
{
    int t1 = 1000;
    long long t2 = 234324234234234;
    float t3 = 100.234f;
    double t4 = 3242342.323242324;
    char tc1[15];
    char tc2[30];
    char tc3[15];
    char tc4[50];
    sprintf(tc1,"%d", t1);
    printf("%s\n", tc1);

    sprintf(tc2,"%lld", t2);
    printf("%s\n", tc2);

    sprintf(tc3,"%f", t3);
    printf("%s\n", tc3);

    sprintf(tc4,"%lf", t4);
    printf("%s\n", tc4);
}
输出结果如下

1000
234324234234234
100.234001
3242342.323242

五、用stringstream将基本类型转成字符串

stringstream对基本类型转换到字符串时,不会出现sprintf的问题,内部使用的std::string来存储,效率上会低一些。注意,double输出采用的是科学计数法。
void test()
{
    int t1 = 1000;
    long long t2 = 234324234234234;
    float t3 = 100.234f;
    double t4 = 3242342.323242324;
    stringstream stream1;
    stringstream stream2;
    stringstream stream3;
    stringstream stream4;
    stream1 << t1;
    printf("%s\n", stream1.str().c_str());

    stream2 << t2;
    printf("%s\n", stream2.str().c_str());

    stream3 << t3;
    printf("%s\n", stream3.str().c_str());

    stream4 << t4;
    printf("%s\n", stream4.str().c_str());
}
输出结果如下

1000
234324234234234
100.234
3.24234e+006

六、用lexical_cast做基本类型、字符串间转换

lexical_cast支持类型间互转,不过不能将基本类型直接转成char[],需要先转成string,再转成char[]。而且lexical_cast在类型不能转换时,会抛出异常。
void test()
{
        float t = 100.234f;
        string s = "343.323";
        try
        {
            printf("%s\n", boost::lexical_cast<string>(t).c_str());
            printf("%f\n", boost::lexical_cast<float>(s));
            printf("%.10lf %.10f\n",boost::lexical_cast<double>(t),t);
        }
        catch (boost::bad_lexical_cast* e)
        {
            printf("%s\n",e->what());
        }
}
输出结果如下
100.234001
343.322998
100.2340011597 100.2340011597

七、总结

1、做字符串到基本类型转换时,可以采用atoi,atof等函数,但需要注意字符串和要转换的类型不符合时,也能转换,用lexical_cast函数则要求能正确转换,否则会抛出异常,对异常需要做处理。
2、做基本类型到字符串转转换时,可以用sprintf,stringstream,lexical_cast,不建议用非标准的itoa函数。sprintf适合转成C风格字符串,但需要分配足够的缓冲区,否则会写坏其他内存,而lexical_cast不存在这个问题,但不能直接转换成char[],在没用boost库的时候可以采用stringstream。
3、lexical_cast还能支持基本类型间的互转,在将double,float互转不会报错,但long long 转 int时,如果超出int的范围会抛异常,而float转int时,当转换的误差超过一定值也会抛异常。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值