Converting a string to an integer

A

Alex Neumann

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...
 
L

Leor Zolman

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
 
D

David Harmon

On Mon, 10 May 2004 20:31:03 GMT in comp.lang.c++, Leor Zolman
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()?
 
M

marbac

Alex said:
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
//
 
J

Juergen Heinzl

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
 
R

Ron Natalie

marbac said:
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;
 
L

Leor Zolman

On Mon, 10 May 2004 20:31:03 GMT in comp.lang.c++, Leor Zolman


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top