Convert str to int.

Aug 15, 2014 at 8:58pm
Hi everyone. Also a very stupid question: which function does convert string to int? Like strtoint in Pascal. And which converts them backwards?

Last edited on Aug 15, 2014 at 8:59pm
Aug 15, 2014 at 9:04pm
you can use atoi:

atoi( str.c_str() )
Aug 15, 2014 at 10:11pm
You could also use a stringstream.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
#include <sstream>

int main()
{   
    std::string myStr = "123456789";
    int myInt = 0;
    std::istringstream os(myStr);
    os >> myInt;

    std::cout << myInt << std::endl;

    return 0;
}
123456789


http://www.cplusplus.com/reference/sstream/istringstream/
Last edited on Aug 15, 2014 at 10:12pm
Aug 16, 2014 at 1:06pm
or:
1
2
string str = "123";
int i = stoi(str);
Aug 19, 2014 at 9:44am
THanks a lot!)
Aug 19, 2014 at 6:01pm
closed account (SECMoG1T)
Also you might also like to use the stringstream objects , i mean they are very friendly to me when a want conversion from any type to any other
Topic archived. No new replies allowed.