Connecting Tech Pros Worldwide Help | Site Map

Converting a string to an integer

Alex Neumann
Guest
 
Posts: n/a
#1: Jul 22 '05
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...
Leor Zolman
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Converting a string to an integer


On 10 May 2004 20:18:25 GMT, Alex Neumann <nospam@web.de> wrote:
[color=blue]
>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 ;-)[/color]

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
David Harmon
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Converting a string to an integer


On Mon, 10 May 2004 20:31:03 GMT in comp.lang.c++, Leor Zolman
<leor@bdsoft.com> wrote,
[color=blue]
> return std::sscanf(intext, "%d", &result);[/color]

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

marbac
Guest
 
Posts: n/a
#4: Jul 22 '05

re: Converting a string to an integer


Alex Neumann schrieb:[color=blue]
> Hi,
>
> I need a function which converts an string to an integer.[/color]

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.

[color=blue]
> Has someone a better idea ?
> Only ISO C++ please, no extra libs ;-)[/color]

Found in cstdlib (of the GNU ISO C++ Library):
//
// ISO C++ 14882: 20.4.6 C library
//

[color=blue]
> Thanks...[/color]
Juergen Heinzl
Guest
 
Posts: n/a
#5: Jul 22 '05

re: Converting a string to an integer


In article <2ga6chFdunbU1@uni-berlin.de>, Alex Neumann wrote:[color=blue]
> 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;[/color]
[-]
-1 and +1 are valid integers, but neither "-" nor "+" are
digits..
[color=blue]
> while (*digit >= '0' && *digit <='9')
> {
> result = (result * 10) + (*digit - '0');[/color]
[-]
1029384003948593093849 probably isn't a valid integer, yet
you don't catch overflows here.
[-][color=blue]
> Has someone a better idea ?[/color]
[-]
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 : juergen@manannan.org \ send money instead /
\ Photo gallery : www.manannan.org \ /
Ron Natalie
Guest
 
Posts: n/a
#6: Jul 22 '05

re: Converting a string to an integer



"marbac" <marbac@chello.at> wrote in message news:t8Snc.60836$O9.26010@news.chello.at...
[color=blue]
> int atoi ( const char * string );
>[/color]
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;

Leor Zolman
Guest
 
Posts: n/a
#7: Jul 22 '05

re: Converting a string to an integer


On Mon, 10 May 2004 21:04:53 GMT, David Harmon <source@netcom.com> wrote:
[color=blue]
>On Mon, 10 May 2004 20:31:03 GMT in comp.lang.c++, Leor Zolman
><leor@bdsoft.com> wrote,
>[color=green]
>> return std::sscanf(intext, "%d", &result);[/color]
>
>While sscanf() certainly has a place, for converting a single number
>why not omit the format business and call strtol() or atoi()?[/color]

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
Closed Thread