Computer Science Canada

String to int

Author:  Chris [ Sat Jun 10, 2006 11:44 am ]
Post subject:  String to int

I was wondering if there is anyway of changing a string value to an int value. Thanks for any help in advance.

Author:  wtd [ Sat Jun 10, 2006 11:54 am ]
Post subject: 

Stringstreams can do the job reasonably well.

Author:  Chris [ Sat Jun 10, 2006 12:11 pm ]
Post subject: 

wtd wrote:
Stringstreams can do the job reasonably well.
Can you explain this a little more.. thanks

Author:  wtd [ Sat Jun 10, 2006 1:37 pm ]
Post subject: 

Google for "c++ stringstream".

I'm happy to answer questions, but you should do some of your own research first.

Author:  Panopticon [ Sun Aug 06, 2006 12:33 am ]
Post subject: 

atoi(); works fine. Man page for said function

Author:  Andy [ Sun Aug 06, 2006 10:39 am ]
Post subject: 

atoi is used for character arrays, converting a string adt to an character array then passing it through atoi is no where near as efficient as using the stringstream approach wtd suggested

Author:  Panopticon [ Sun Aug 06, 2006 11:23 am ]
Post subject: 

Andy wrote:
atoi is used for character arrays, converting a string adt to an character array then passing it through atoi is no where near as efficient as using the stringstream approach wtd suggested


I assumed that he meant character arrays when he said string. If the string is in an ADT then you're right: using atoi is quite inefficient.

Author:  bugzpodder [ Sun Aug 06, 2006 1:23 pm ]
Post subject: 

Andy wrote:
atoi is used for character arrays, converting a string adt to an character array then passing it through atoi is no where near as efficient as using the stringstream approach wtd suggested

string objects probably use internal char array representation, so it is pretty efficient.

Author:  bugzpodder [ Sun Aug 06, 2006 1:24 pm ]
Post subject: 

and stringstream probably have a lot more overhead than atoi

Author:  wtd [ Sun Aug 06, 2006 1:34 pm ]
Post subject: 

bugzpodder wrote:
and stringstream probably have a lot more overhead than atoi


Well, a lot of this depends on the language you're using.

Using C? Then you can't use stringstream.

Using C++? Then you should be using stringstream.

Author:  UnsignedChar [ Mon Dec 18, 2006 4:29 pm ]
Post subject: 

How about just multiplying by 10 and subtracting the character value?
Like in this example:

code:

#include <stdio.h>
#include <string.h>
       
int main(void)
{       
        char str[] = "123456789";
        int  num = 0, i;
                                 
        for(i = 0; i < strlen(str); i++){
                num *= 10;
                num += (str[i] - '0');
        }       
                         
        printf("%d", num);               
        return 0;
}

Author:  Monstrosity_ [ Mon Dec 18, 2006 4:46 pm ]
Post subject: 

Panopticon wrote:
atoi(); works fine. Man page for said function

No it doesn't, even if he were using an array of characters.

Panopticon wrote:
#include <stdio.h>
#include <string.h>

int main(void)
{
char str[] = "123456789";
int num = 0, i;

for(i = 0; i < strlen(str); i++){
num *= 10;
num += (str[i] - '0');
}

printf("%d", num);
return 0;
}

This is fine until you let the user enter some data. What if its negative, contains non-numeric characters, overflows, ect..

Author:  Clayton [ Mon Dec 18, 2006 4:54 pm ]
Post subject: 

Holy necro-post Batman!

Welcome to CompSci.ca UnsignedChar! It's nice to see you contributing, but this topic is over 3 months old. Just be sure to check the dates next time Very Happy

Also, like Monstrosity_ said, what if the user enters some weird output? Then you get an error, not good.

Author:  UnsignedChar [ Mon Dec 18, 2006 6:14 pm ]
Post subject: 

Freakman wrote:

Welcome to CompSci.ca UnsignedChar!


Thank you.

Freakman wrote:

It's nice to see you contributing, but this topic is over 3 months old. Just be sure to check the dates next time Very Happy


No problem, I can deal with that....only realize that i can go back two pages worth of topics and hit last year, it'll be hard not to break this rule.

Also, (not trying to be a jerk) but topics that are old does not mean they can not be contributed to further, perhaps if the topic were a dead or solved subject then it would be inappropriate.

Freakman wrote:

Also, like Monstrosity_ said, what if the user enters some weird output? Then you get an error, not good.


Here's a good example of my point, this topic is now extended by some interesting questions.


: