Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::string to a uint64_t

Tags:

c++

boost

c++98

How do I convert a std::string to an uint64_t? I would also like the function to throw if the string does not contain a pure representation of an uint64_t. For example, conversions of these should throw:

"NO: 9889"
"9889L"
"9889U"
"1e+22"

or indicate by another means that they are not a pure representation. I am using C++98 and boost (including boost::regex).

like image 757
Baz Avatar asked Jan 13 '23 11:01

Baz


1 Answers

Use boost::lexical_cast. It will throw a bad_lexical_cast if the number can't be parsed or overflows.

If you ever switch to C++11, there's a new function in the standard for that which should be faster.

like image 174
Sebastian Redl Avatar answered Jan 22 '23 04:01

Sebastian Redl