2

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

4 Answers 4

3

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);

Sign up to request clarification or add additional context in comments.

1 Comment

But very readable (and well named) :-) PS: referring to lexical_cast
1

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)); } 

4 Comments

assuming 4 bytes for integer, there will be 8 hex digits with the "%x" specifier.
Am I missing something here? sprintf(buf, "%x", 42); puts "2a" into buf. atoi("2a") is going to return 2 not 42.
@D.Shawley: You caught me on that one. Have used strtol instead
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
0

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; } 

6 Comments

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: 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.
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.
Why the downvote? I voted up, it gets the point across just fine, even if the headers/main function is questionable.
Thanks Ashley. Appreciate your response.
|
-1

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

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.