Author |
Message |
Chris
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
wtd
|
Posted: Sat Jun 10, 2006 11:54 am Post subject: (No subject) |
|
|
Stringstreams can do the job reasonably well. |
|
|
|
|
![](images/spacer.gif) |
Chris
|
Posted: Sat Jun 10, 2006 12:11 pm Post subject: (No subject) |
|
|
wtd wrote: Stringstreams can do the job reasonably well. Can you explain this a little more.. thanks |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Sat Jun 10, 2006 1:37 pm Post subject: (No subject) |
|
|
Google for "c++ stringstream".
I'm happy to answer questions, but you should do some of your own research first. |
|
|
|
|
![](images/spacer.gif) |
Panopticon
|
|
|
|
![](images/spacer.gif) |
Andy
|
Posted: Sun Aug 06, 2006 10:39 am Post subject: (No 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 |
|
|
|
|
![](images/spacer.gif) |
Panopticon
|
Posted: Sun Aug 06, 2006 11:23 am Post subject: (No 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. |
|
|
|
|
![](images/spacer.gif) |
bugzpodder
![](http://www.vbforums.com/avatar.php?userid=31755&dateline=1038631511)
|
Posted: Sun Aug 06, 2006 1:23 pm Post subject: (No 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. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
bugzpodder
![](http://www.vbforums.com/avatar.php?userid=31755&dateline=1038631511)
|
Posted: Sun Aug 06, 2006 1:24 pm Post subject: (No subject) |
|
|
and stringstream probably have a lot more overhead than atoi |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Sun Aug 06, 2006 1:34 pm Post subject: (No 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. |
|
|
|
|
![](images/spacer.gif) |
UnsignedChar
|
Posted: Mon Dec 18, 2006 4:29 pm Post subject: (No 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;
} |
|
|
|
|
|
![](images/spacer.gif) |
Monstrosity_
|
Posted: Mon Dec 18, 2006 4:46 pm Post subject: (No subject) |
|
|
Panopticon wrote:
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.. |
|
|
|
|
![](images/spacer.gif) |
Clayton
![](http://compsci.ca/v3/uploads/user_avatars/1718239683472e5c8d7e617.jpg)
|
Posted: Mon Dec 18, 2006 4:54 pm Post subject: (No 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
Also, like Monstrosity_ said, what if the user enters some weird output? Then you get an error, not good. |
|
|
|
|
![](images/spacer.gif) |
UnsignedChar
|
Posted: Mon Dec 18, 2006 6:14 pm Post subject: (No 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
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. |
|
|
|
|
![](images/spacer.gif) |
|