How to cast form string to number safely?

Dear boost-users: I am really sorry for asking shch a silly question, but I really need your help! boost::lexical_cast is a great function to do casting. If I want is to cast from string to a "long long" type value, I can use the follow: long long llVar = lexical_cast<long long>("123456789"); It is alright in this circs, but what if: lexical_cast<long long>("9999999999999999999999999999999999999999999999999999999999999999999999999999"); This is an obvious overflow, but I have no way to handle this error, it returns a wrong number without throwing an exception! Any suggestion? Thank you very much!!! Kevin

袁晓辉 wrote:
Dear boost-users:
I am really sorry for asking shch a silly question, but I really need your help!
boost::lexical_cast is a great function to do casting. If I want is to cast from string to a "long long" type value, I can use the follow:
long long llVar = lexical_cast<long long>("123456789");
It is alright in this circs, but what if:
lexical_cast<long long>("9999999999999999999999999999999999999999999999999999999999999999999999999999");
This is an obvious overflow, but I have no way to handle this error, it returns a wrong number without throwing an exception!
Any suggestion? Thank you very much!!! Kevin
------------------------------------------------------------------------
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
I tested your example with VC++ 7.1, and it throws a bad_lexical_cast. The problem you have has nothing to do with lexical_cast. lexical_cast uses streams to do the conversion. It seems that you use an implementation of the C++ standard library with poorly implemented streams. I see two options: 1. Write your own conversion routine by hand. (Not that hard.) 2. Get a better implementation of the C++ standard library. You can not use atoi, for its behaviour is unspecified when the conversion can not be carried out. --Johan Råde

Is it possible that you have disabled exceptions in your project? Regards, Joachim
Dear boost-users:
I am really sorry for asking shch a silly question, but I really need your help!
boost::lexical_cast is a great function to do casting. If I want is to cast from string to a "long long" type value, I can use the follow:
long long llVar = lexical_cast<long long>("123456789");
It is alright in this circs, but what if:
lexical_cast<long long>("9999999999999999999999999999999999999999999999999999999 999999999999999999999");
This is an obvious overflow, but I have no way to handle this error, it returns a wrong number without throwing an exception!
Any suggestion? Thank you very much!!!
Kevin

Thank you very much!
I have two points now:
1. There may be something "improper" in the implementation of the C++
standard library I used:
#include <sstream>
std::stringstream ss;
ss << "9999999999999999999999999999999999999999";
long long llVar = 0;
if(!(ss >> llVar))
{
std::cout << "wrong!" << endl;
}
else
{
std::cout << llVar << endl;
};
Instead of printing "wrong!" it gives me nonsense(9223372036854775807). But
if I change the type of llVar to "long", it will report an error as
expected. It seems that only "long long" type has problem with stringstram
here(I am using MS VS2005).
2. I do not disable exception in my project, because if I use type "long"
with lexical_cast, it will throw a bad_lexical_cast exception when overflow
occured.
2006/7/26, Meißner, Joachim [Rohmann GmbH]
Is it possible that you have disabled exceptions in your project?
Regards, Joachim
Dear boost-users:
I am really sorry for asking shch a silly question, but I really need your help!
boost::lexical_cast is a great function to do casting. If I want is to cast from string to a "long long" type value, I can use the follow:
long long llVar = lexical_cast<long long>("123456789");
It is alright in this circs, but what if:
lexical_cast<long long>("9999999999999999999999999999999999999999999999999999999 999999999999999999999");
This is an obvious overflow, but I have no way to handle this error, it returns a wrong number without throwing an exception!
Any suggestion? Thank you very much!!!
Kevin
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

袁晓辉 wrote:
Thank you very much!
I have two points now: 1. There may be something "improper" in the implementation of the C++ standard library I used:
#include <sstream> std::stringstream ss; ss << "9999999999999999999999999999999999999999"; long long llVar = 0; if(!(ss >> llVar)) { std::cout << "wrong!" << endl; } else { std::cout << llVar << endl; }; Instead of printing "wrong!" it gives me nonsense(9223372036854775807). But if I change the type of llVar to "long", it will report an error as expected. It seems that only "long long" type has problem with stringstram here(I am using MS VS2005).
2. I do not disable exception in my project, because if I use type "long" with lexical_cast, it will throw a bad_lexical_cast exception when overflow occured.
2006/7/26, Meißner, Joachim [Rohmann GmbH] < meissner@rohmann.de mailto:meissner@rohmann.de>:
Is it possible that you have disabled exceptions in your project?
Regards, Joachim > Dear boost-users: > > I am really sorry for asking shch a silly question, but I > really need your help! > > boost::lexical_cast is a great function to do casting.. If I > want is to cast from string to a "long long" type value, I > can use the follow: > > long long llVar = lexical_cast<long long>("123456789"); > > It is alright in this circs, but what if: > > lexical_cast<long > long>("9999999999999999999999999999999999999999999999999999999 > 999999999999999999999"); > > This is an obvious overflow, but I have no way to handle this > error, it returns a wrong number without throwing an exception! > > Any suggestion? Thank you very much!!! > > Kevin > > _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org mailto:Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
------------------------------------------------------------------------
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
I ran the following program on MSVS 2003 ------------------------------------------ #include "boost/lexical_cast.hpp" int main() { long long a = boost::lexical_cast<long long>("9999999999999999999999999999999999999999999999999999999999999999999999999999"); } --------------------------------------------- and it throws a bad_lexical_cast. And you says it does not throw on MSVS 2005. Looks like Microsoft has messed up. You might have to switch back to MSVS 2003 or roll your own conversion function. --Johan Råde

I think you need STLport. Compile it with boost.
On 7/26/06, 袁晓辉
Dear boost-users:
I am really sorry for asking shch a silly question, but I really need your help!
boost::lexical_cast is a great function to do casting. If I want is to cast from string to a "long long" type value, I can use the follow:
long long llVar = lexical_cast<long long>("123456789");
It is alright in this circs, but what if:
lexical_cast<long long>("9999999999999999999999999999999999999999999999999999999999999999999999999999");
This is an obvious overflow, but I have no way to handle this error, it returns a wrong number without throwing an exception!
Any suggestion? Thank you very much!!! Kevin
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Sincerely, liang
participants (4)
-
Johan Råde
-
Liang Chen
-
Meißner, Joachim [Rohmann GmbH]
-
袁晓辉