Jump to content
  • Advertisement

Archived

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

String to int

This topic is 6080 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

Advertisement
quote:
Original post by ZeroBit
char string[255];

int i = some integer number
sprintf (string, "%d", i);


That would be the opposite conversion (int -> string).

rather do:
   
char * string = "12343"; // some string representing an integer.


int i;
sscanf(string, "%i", &i);

Share this post


Link to post
Share on other sites
A string can be converted to an int, with a cast... eg.

#include
char string[]="12345";

main () {
int x=(int)string;
cout << x;
return 0;
}

Share this post


Link to post
Share on other sites
quote:
Original post by dmounty
A string can be converted to an int, with a cast... eg.

#include
char string[]="12345";

main () {
int x=(int)string;
cout << x;
return 0;
}


This will compile but won''t give the expected result actually. At the end of the program x will contain the starting address of the string. The simplest way to obtain an int from a string is to do :

int x = atoi(string);

Note : you may need to include a header to use the atoi() function (maybe stdlib.h).

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!