tags:

views:

127

answers:

4

Hi,

I would like to know what is the easiest way to convert an int to C++ style string and from C++ style string to int.

edit

Thank you very much. When converting form string to int what happens if I pass a char string ? (ex: "abce").

Thanks & Regards,

Mousey

+2  A: 

Probably the easiest is to use operator<< and operator>> with a stringstream (you can initialize a stringstream from a string, and use the stream's .str() member to retrieve a string after writing to it.

Boost has a lexical_cast that makes this particularly easy (though hardly a paragon of efficiency). Normal use would be something like int x = lexical_cast<int>(your_string);

Jerry Coffin
But very readable (and well named) :-) PS: referring to lexical_cast
Martin York
A: 

Use atoi to convert a string to an int. Use a stringstream to convert the other way.

dan04
If you want something on the order of `atoi`, at least use `strtol`, which can at least indicate when there's an error.
Jerry Coffin
A: 

This is to convert string to number.

#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>

int convert_string_to_number(const std::string& st)
{
    std::istringstream stringinfo(st);
    int num = 0;
    stringinfo >> num;
    return num;
}



int main()
{
    int  number = 0;

    std::string number_as_string("425");
    number = convert_string_to_number(number_as_string);

    std::cout << "The number is " << number << std::endl;
    std::cout << "Number of digits are " << number_as_string.length() << std::endl;
}

Like wise, the following is to convert number to string.

#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>

std::string convert_number_to_string(const int& number_to_convert)
{
    std::ostringstream os;
    os << number_to_convert;
    return (os.str());
}

int main()
{
    int number = 425;

    std::string stringafterconversion;
    stringafterconversion = convert_number_to_string(number);

    std::cout << "After conversion " << stringafterconversion << std::endl;
    std::cout << "Number of digits are " << stringafterconversion.length() << std::endl;
}
Ick... `#include "stdafx.h"`? You need the other three, but you can definitely do without `stdafx.h`. Oh, and `main` is supposed to return an integer.
D.Shawley
@D. Shawley: c++ actually has a rule saying that `main` is a special case and may have the `return 0;` omitted resulting in the same effect as if you had put it there.
Evan Teran
What happens if I call the first function like this: `convert_string_to_number("blah")`? As it is, this will silently fail and return the same as if I'd called `convert_string_to_number("0")`. That''s a definitive `-1` from me.
sbi
Why the downvote? I voted up, it gets the point across just fine, even if the headers/main function is questionable.
AshleysBrain
Thanks Ashley. Appreciate your response.
@AshleysBrain: I wrote "That's a definitive `-1` from me" and write below that you come and ask "Why the downvote?" Frankly, I'm at a loss as to what to answer to that politely.
sbi
+1  A: 

You can change "%x" specifier to "%d" or any other format supported by sprintf. Ensure to appropriately adjust the buffer size 'buf'

int main(){
        char buf[sizeof(int)*2 + 1];
        int x = 0x12345678;
        sprintf(buf, "%x", x);

        string str(buf);

        int y = atoi(str.c_str());
    }

EDIT 2:

int main(){
    char buf[sizeof(int)*2 + 1];
    int x = 42;
    sprintf(buf, "%x", x);

    string str(buf);

    //int y = atoi(str.c_str());
    int y = static_cast<int>(strtol(str.c_str(), NULL, 16));
}
Chubsdad
why sizeof(int)*2 ?
mousey
assuming 4 bytes for integer, there will be 8 hex digits with the "%x" specifier.
Chubsdad
Am I missing something here? `sprintf(buf, "%x", 42);` puts "2a" into `buf`. `atoi("2a")` is going to return `2` not `42`.
D.Shawley
@D.Shawley: You caught me on that one. Have used strtol instead
Chubsdad
while this is a functional solution, I try to steer developers who are clearly fairly new to the language right away from using unchecked fixed sized char arrays, its just asking for segfaults.If performance is an issue, char's might be a better way to work but otherwise i'd be sticking with stringstreams
Fuzz