小编Art*_*hur的帖子

boost::lexical_cast 可以将字符串内的十六进制转换为整数吗?

我在本主题中看到C++ 将十六进制字符串转换为有符号整数boost::lexical_cast可以将字符串内的十六进制转换为另一种类型(int、long...)

但是当我尝试这段代码时:

std::string s = "0x3e8";

try {
    auto i = boost::lexical_cast<int>(s);
    std::cout << i << std::endl;        // 1000
}
catch (boost::bad_lexical_cast& e) {
    // bad input - handle exception
    std::cout << "bad" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

它以错误的词法转换异常结束!

boost 不支持这种从 string hex 到 int 的转换?

c++ boost lexical-cast

5
推荐指数
1
解决办法
904
查看次数

标签 统计

boost ×1

c++ ×1

lexical-cast ×1