By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,134 Members | 1,742 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,134 IT Pros & Developers. It's quick & easy.

Converting a string to an integer

P: n/a
Hi,

I need a function which converts an string to an integer.
Currently I have this:

bool string2int(char* digit, int& result)
{
result = 0;

if (!(*digit >= '0' && *digit <='9'))
return false;
while (*digit >= '0' && *digit <='9')
{
result = (result * 10) + (*digit - '0');
digit++;
}

if (*digit != 0 && *digit != ',' && *digit != ']')
{
return false;
}

return true;
}

Has someone a better idea ?
Only ISO C++ please, no extra libs ;-)

Thanks...
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On 10 May 2004 20:18:25 GMT, Alex Neumann <no****@web.de> wrote:
Hi,

I need a function which converts an string to an integer.
Currently I have this:

bool string2int(char* digit, int& result)
{
result = 0;

if (!(*digit >= '0' && *digit <='9'))
return false;
while (*digit >= '0' && *digit <='9')
{
result = (result * 10) + (*digit - '0');
digit++;
}

if (*digit != 0 && *digit != ',' && *digit != ']')
{
return false;
}

return true;
}

Has someone a better idea ?
Only ISO C++ please, no extra libs ;-)


Well, you're doing things the hard way and not being as flexible as
facilities you already have in your libraries. If you want to code it by
hand, you could be using <cctype> facilities (e.g. isdigit), checking for
leading white space, etc. Note that C libs are also ISO C++, so my
suggestion to you would be to let the libs do all the heavy lifting:

#include <cstdio>
bool string2int(const char *intext, int &result)
{
return std::sscanf(intext, "%d", &result);
}

-leor

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2

P: n/a
On Mon, 10 May 2004 20:31:03 GMT in comp.lang.c++, Leor Zolman
<le**@bdsoft.com> wrote,
return std::sscanf(intext, "%d", &result);


While sscanf() certainly has a place, for converting a single number
why not omit the format business and call strtol() or atoi()?

Jul 22 '05 #3

P: n/a
Alex Neumann schrieb:
Hi,

I need a function which converts an string to an integer.
stdlib.h (cstdlib):

int atoi ( const char * string );

Convert string to integer.
Parses string interpreting its content as a number and returns an int
value.

Has someone a better idea ?
Only ISO C++ please, no extra libs ;-)
Found in cstdlib (of the GNU ISO C++ Library):
//
// ISO C++ 14882: 20.4.6 C library
//

Thanks...

Jul 22 '05 #4

P: n/a
In article <2g***********@uni-berlin.de>, Alex Neumann wrote:
Hi,

I need a function which converts an string to an integer.
Currently I have this:

bool string2int(char* digit, int& result)
{
result = 0;

if (!(*digit >= '0' && *digit <='9'))
return false; [-]
-1 and +1 are valid integers, but neither "-" nor "+" are
digits..
while (*digit >= '0' && *digit <='9')
{
result = (result * 10) + (*digit - '0'); [-]
1029384003948593093849 probably isn't a valid integer, yet
you don't catch overflows here.
[-] Has someone a better idea ?

[-]
http://www.google.com/ -- search for strtol.c and modify
the code to your needs.

Cheers,
Juergen

--
\ Real name : Juergen Heinzl \ no flames /
\ Email private : ju*****@manannan.org \ send money instead /
\ Photo gallery : www.manannan.org \ /
Jul 22 '05 #5

P: n/a

"marbac" <ma****@chello.at> wrote in message news:t8******************@news.chello.at...
int atoi ( const char * string );

Atoi has undefined behavior when fed certain inputs. The strtoX functions
are better behaved.

There's always stringstreams and boost's lexical cast as well.

string str("42");
instringstream is(str);
int i;
is >> i;

Jul 22 '05 #6

P: n/a
On Mon, 10 May 2004 21:04:53 GMT, David Harmon <so****@netcom.com> wrote:
On Mon, 10 May 2004 20:31:03 GMT in comp.lang.c++, Leor Zolman
<le**@bdsoft.com> wrote,
return std::sscanf(intext, "%d", &result);


While sscanf() certainly has a place, for converting a single number
why not omit the format business and call strtol() or atoi()?


Sure, those seem like more efficient choices in this case. I still like to
use sscanf because it is so flexible (if the types involved get changed,
there are more variations in format conversions than there are specialized
library functions for each type), and the savings in terms of time/space
seem like a drop in the bucket in the general scheme of things.

In this particular post, however, I was simply trying to convey the idea
of employing /some/ standard lib facility instead of hand-coding the
algorithm.
-leor

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.