我在本主题中看到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 的转换?