Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Craazer

string to int

This topic is 5552 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! This is probably often asked queston but the search engine doesn''t work right now... So how can I convert string to int or double? Converting backwards isn''t a problem becose I use wsprintf() for that, or is it a good solution?

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by Craazer
Hi! This is probably often asked queston but the search engine doesn''t work right now...

You could have fooled me!
quote:

So how can I convert string to int or double?

You could try using the `parse-integer'' function.

Share this post


Link to post
Share on other sites
quote:
Original post by SabreMan
You could try using the `parse-integer'' function.

I don''t think he was asking about the lisp function as he said that he was using wsprintf to convert from int to string...



Update GameDev.net system time campaign - success at last

Share this post


Link to post
Share on other sites
quote:
Original post by Craazer
So how can I convert string to int or double?

You have a lot of options here: The atoi function, the boost::lexical_cast operator, the stringstream class are only three of them.
quote:
Converting backwards isn''t a problem becose I use wsprintf() for that, or is it a good solution?

wsprintf is Windows only, sprintf (or snprintf) is more portable. There are a lot of options for you here as well: the boost::lexical_cast operator, the stringstream class are only a few of them.



Update GameDev.net system time campaign - success at last

Share this post


Link to post
Share on other sites
quote:
Original post by Craazer
Hi! This is probably often asked queston but the search engine doesn''t work right now...
Google does. ''"string to int" c'' gave immediately results. If people only did at least the minimum amount of searching before asking... maybe Gamedev forums wouldn''t exist then though.

Share this post


Link to post
Share on other sites
quote:
Original post by civguy
[quote]Original post by Craazer
Hi! This is probably often asked queston but the search engine doesn''t work right now...
Google does. ''"string to int" c'' gave immediately results. If people only did at least the minimum amount of searching before asking... maybe Gamedev forums wouldn''t exist then though.

Probably would''t and the bad thing when searhing whit google is that strings and ints are in many langueges like java (what was the first search result).

However it seems that stringstream class is a good solution...
So great I can use that then, but when posting this thread I was hoping to learn about the actual technique as practice.

And when I said string I actualy ment string types and char arrays or pointers as strings, but I gues you can copy char arrays to string types.


Share this post


Link to post
Share on other sites
just.. do it yourself..

read the first char. is it a number, get the number out of it. read next char, if its a number, multiply current value by 10, add new number to it, etc..


  
bool isDigit(char chr) { return chr >= ''0'' && chr <= ''9''; }

char* chr = beginning_of_string;
while(*chr) { if(isDigit(chr)) break; ++chr; }
int number = 0;
do {
number *= 10;
number += *chr - ''0'';
chr++;
}while(*chr && isDigit(chr));


something like this. i''m very very very tierd..:D

"take a look around" - limp bizkit
www.google.com

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!