用boost::lexical_cast进行数值转换

在STL库中,我们可以通过stringstream来实现字符串和数字间的转换:

int i = 0;
    stringstream ss;

ss << "123";
    ss >> i;

但stringstream是没有错误检查的功能,例如对如如下代码,会将i给赋值为12.

ss << "12.3";
    ss >> i;

甚至连这样的代码都能正常运行:

ss << "hello world";
    ss >> i;

这显然不是我们所想要看到的。为了解决这一问题,可以通过boost::lexical_cast来实现数值转换:

int i = boost::lexical_cast<int>("123");
    double d = boost::lexical_cast<double>("12.3");

对于非法的转换,则会抛异常:

try
    {
        int i = boost::lexical_cast<int>("12.3");
    }
    catch (boost::bad_lexical_cast& e)
    {
        cout << e.what() << endl;
    }

对于16机制数字的转换,可以以如下方式进行:

template <typename ElemT>
    struct HexTo {
        ElemT value;
        operator ElemT() const {return value;}
        friend std::istream& operator>>(std::istream& in, HexTo& out) {
            in >> std::hex >> out.value;
            return in;
        }
    };

int main(void) 
    {
        int x = boost::lexical_cast<HexTo<int>>("0x10"); 
    }

时间: 2024-08-25 04:24:51

用boost::lexical_cast进行数值转换的相关文章

boost::lexical_cast

boost::lexical_cast为数值之间的转换(conversion)提供了一揽子方案,比如:将一个字符串"123"转换成整数123,代码如下: string s = "123"; int a = lexical_cast<int>(s); 这种方法非常简单,笔者强烈建议大家忘掉std诸多的函数,直接使用boost:: lexical_cast.如果转换发生了意外,lexical_cast会抛出一个bad_lexical_cast异常,因此程序中

boost::lexical_cast常见用法详解之万能转换

提示: 虽然在c中可是使用类似于atoi之类的函数对字符串转换成整型,但是我们在这儿还是推荐使用这个函数 如果转换发生了错误,lexical_cast会抛出一个bad_lexical_cast异常,因此程序中需要对其进行捕捉. 下面是程序示例: #include <iostream> #include <boost/lexical_cast.hpp> using namespace std; using namespace boost; int main() { string s

boost.lexical_cast 学习

1,字符串 到 数值类型的转换 2,数值 到 字符串的转换 3,异常处理情况 4,boost::lexical_cast 的原型: template<typename Target, typename Source>     Target lexical_cast(Source arg); lexical_cast 是依赖于字符串流 std::stringstream 的,其原理也是相当的简单:把源类型 (Source) 读入到字符流中,再写到目标类型 (Target) 中.但这里同时也带来了

Boost::Lexical_cast 的使用

1.C++代码 #include <boost/lexical_cast.hpp> #include <iostream> int main() { using boost::lexical_cast; int a = lexical_cast<int>("123"); double b = lexical_cast<double>("123.12"); std::cout<<a<<std::e

Boost::lexical_cast类型转换

1.字符串->数值 C++代码 1 #include <boost/lexical_cast.hpp> 2 #include <iostream> 3 int main() 4 { 5 using boost::lexical_cast; 6 int a = lexical_cast<int>("123"); 7 double b = lexical_cast<double>("123.12"); 8 std::

JavaScript数值转换总结

在JavaScript中,数值转换一般有三种方式: 一.Number(param)函数:param可以用于任何数据类型 1.1  param是Boolean值,true和false分别转换为1和0: 1.2  param是数值,只是简单的传入和返回 1.3  param是null和undefined,分别返回0和NaN 1.4  param是字符串,遵循下列规则: 1.4.1  如果字符串中只包含数字,则转换为十进制,前导0被忽略: 1.4.2  如果字符串中包含有效的浮点数格式,则返回对应的浮

〖Linux〗Shell十进制数值转换十六进制

1 dec2hex(){ 2 printf "%x" $1 3 } 4 5 a=$(dec2hex 2131165531) 6 echo $a [Linux]Shell十进制数值转换十六进制,布布扣,bubuko.com

char类型的数值转换

在视频教程中,你已经认识到了数字类型之间.字符串和其他类型之间的转换.而某些时候,我们还需要将char类型转换为int类型,或者把int类型转换为char类型. 这篇文章,将介绍在代码中虽然不太常用,但也需要了解的知识. char类型的数值转换 char转为int int转为char 字符数据的运算 char转为int 一个字符 '汉' 怎么可能转换为数字呢? 实际上是可以的,在之前的补充资料中已经说到,计算机对字符的存储,是使用某种编码规则对应的数字来存储的. 在C#语言中,使用Unicode

javascript 的数值转换

js中有3个函数可以把非数值转换成数值:Number(),parseInt()和parseFloat(). Number()可以用于任何数据类型,而另外两个函数则专门用于把字符串转换成数值.这三个函数对于同样的输入会有返回不同的结果. Number()函数的转换规则如下: 如果是Boolean值,则转换成1或者0. 如果是数字,只是简单的传入传出. 如果是null,返回0. 如果是undefined,返回NaN. 如果是字符串,如果字符串中包含非数字字符,则将其转成NaN. 如果是对象,则调用对