-
February 24th, 2009, 09:05 AM
#1
String to int conversion
Hi,
I have read FAQS and used the methods given for string to integer conversion, that works fine but the scenario that i have tested is not being handled. i.e If there is string like "123AA" then all those methods provided in FAQS will convert the first 3 digits and discard the remaining string, and there will be no error. I need a function that should give error in the above case. Please reply if anyone can help me out.
Thanks,
Asif
-
February 24th, 2009, 09:16 AM
#2
Re: String to int conversion
Well you could always check each character to see if it is a numeric or not as in isdigit() or check if c >=0x30 && c < 0x39. Also you could just convert it back to a string, then compare the strings and if the strings are the same you know there were no alphas but if the strings are not the same, you know you had alphas!
HTH,
ahoodin
To keep the plot moving, that's why.
-
February 24th, 2009, 09:25 AM
#3
Re: String to int conversion
strtod() and related functions allow you to get a pointer to the next bit of the string after the conversion. You can test if that points at a NULL terminator, or if there's more valid string remaining.
-
February 24th, 2009, 09:45 AM
#4
Re: String to int conversion
Not tested ... but I think it should work ...
Code:
template <typename T>
bool Convert(const char * s , T & value)
{
std::stringstream ss(s);
ss >> value;
if (!ss)
{
return false;
}
else
{
char c;
ss >> c;
return ss.fail();
}
}
-
February 24th, 2009, 10:00 AM
#5
Re: String to int conversion
Have a look at this thread, which handles the issue on a more generic level. The solution I use is calling s.ignore followed by s.gcount to see if all characters have been read/converted.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
February 24th, 2009, 10:28 AM
#6
Re: String to int conversion
If you are using Boost, then consider using boost::lexical_cast. If not, I would propose a variant of Philip Nicoletti's suggestion (which is actually a variant of what boost::lexical_cast does):
Code:
template<typename T>
bool convert(const std::string& s, T& value)
{
std::stringstream ss(s);
return (ss >> value) && ss.eof();
}
EDIT:
Of course, the assumption here is that you have already read the input into a string, e.g., using std::getline() or the overloaded operator>> for std::string.
Last edited by laserlight; February 24th, 2009 at 10:33 AM.
-
February 24th, 2009, 10:49 AM
#7
Re: String to int conversion
Something like laserlight's solution was my first thought, but
since leading whitespaces are allowed, I would think that
trailing whitespaces should also be allowed.
So, it all depends on what you want to check for.
-
February 25th, 2009, 02:06 AM
#8
Re: String to int conversion
Thank you all for your quick reply and support. I have tested "Philip Nicoletti" and "laserlight" version, both are working fine.
Thanks,
Asif
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|