Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String to int 1

Status
Not open for further replies.

pegazaus

Instructor
Aug 16, 2005
22
SE
Is there a function in C++ other than atoi() that converts a string to int?
 
strtol will, although I prefer a stringstream.

See FAQ #39.2:

You can make that simpler or more complex as needed.
Code:
#include <sstream>
#include <string>

// Returns false if conversion failed, otherwise updates i.
bool convert_to_int(const std::string& s, int& i)
{
    std::istringstream istr(s);
//    istr.unsetf(std::ios::dec); // uncomment this line to allow hex or octal
    if (!(istr >> i))
        return false;

    return istr.rdbuf()->in_avail() == 0;
}
 
I always prefer to do conversions from strings to ints myself as its something that con be done very easily and lets you see exactly whats going on in the conversion.. look at the following:

char myStr[5] = "543";
int i = 0;
int int_value = 0;
//While the characters in the string are numerical
while ((myStr >= '0') && (myStr <= '9'))
{
int_value = int_value * 10;
int_value = int_value + myStr - '0';
i++;
}
 
I prefer built-in methods that have ben designed and tested for these purposes. Your version doesn't handle negative numbers, overflow, octal or hex representation, etc. Plus you cannot tell the difference between a string containing "0" and a string containing an "invalid" number (like "-1").

In my opinion, in general seeing what is going on inside is not nearly as important as using tested and well-known tools.
 
I've almost always had to write my own. Unfortunately these tested and well-known tools do not cater for the European market which use comma as a decimal separator. They are almost exclusively UK/US which take a full stop. Some variants will take commas when you switch locales but those are few and far between.

They also don't cater for input which sometimes use D or ^ instead of E. It is a jungle out there.

I went through various stages of using the more advanced form of sscanf with some strange regular expression but soon gave up because it was just so complex. I'd have Harbison & Steele open on page 318 (yes it is that sad - I still remember the page number 15 years on) flicking too and fro trying to figure it out.

My advice is if you're using small numbers keep to standard routines. If you have to deal with European numbers, very large (15 digits plus) numbers, or strange floating point formats, spin your own or pass them through a filter first to get them in the right format.
 
I supose at the end of the day its just down to whatever you prefer.

There are different situations where both options have their benifits.

Oviously your not going to goto the trouble to write a function that would "handle negative numbers, overflow, octal or hex representation etc", if the built-in methods are fine for what you are doing.

If you are doing something very simple, then 2min worth of code time and you can have yourslef a nice little function that does evrything you want and you always have the option to expand or change it to fit your needs exactly.
Anyway thats just my thought.

You should do whateva suits your needs best.
 
I think int numbers representations are identical in the Europe, US, Japan etc (except financial negatives in parentheses;). The (most) serious defect of RTL number convertors is bad (or very bad;) error handling.
So let's invent (again and again;) home-made convertors for robust user interfaces...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top