converting integer to string C++
Asked Answered
Y

3

1

I am trying to convert an integer to char array and I came across this piece of code

int i = 5;
std::string s;
std::stringstream out;
out << i;
s = out.str();

But when I try to print the value of s it still prints 5. I don't know if its supposed to do that or am I doing something wrong? Besides I would prefer if I could convert the same int to char array. But I would appreciate any help in the matter. Thanks! Code taken from: Alternative to itoa() for converting integer to string C++?

Yeomanly answered 5/4, 2011 at 17:4 Comment(3)
What do you want it to print? The textual representation of the number 5 is "5".Mag
I was just trying to see what the textual representation would be. Does this mean if I try to convert any large numbers to text they would still be the same numbers? For example if I've -635997, what would that look like? Because right now it gives me same numberYeomanly
What else could it be? What is the textual representation of -635997, if not "-635997"? A given number has many possible textual representations; by default, you get the simplest and most familiar (decimal, no leading 0's). There are flags you can set to get other representations.Mag
L
1

Besides I would prefer if I could convert the same int to char array.

char *charPtr = new char[ s.length() + 1 ] ; // s is the string in the snippet posted
strcpy( charPtr, s.c_str() ) ;

// .......

delete[] charPtr ; // Should do this, else memory leak.
Lavadalavage answered 5/4, 2011 at 17:11 Comment(6)
just wondering shouldn't the argument to strlen() be a char pointer? or would the string act the same way?Yeomanly
@Max Eastman : No, you're correct -- passing a std::string to strlen is illegal. It should be new char[s.length() + 1]; or new char[strlen(s.c_str()) + 1];, but of course the second one is inherently less efficient.Voltaism
@Voltaism - You are correct. Time for me to take rest, I guess :)Lavadalavage
@Lavadalavage : std::auto_ptr uses delete rather than delete [], so std::auto_ptr<char> would cause undefined behavior here. On C++0x compilers std::unique_ptr<char[]> would be preferred; on older compilers, boost::scoped_array or boost::shared_array are the best options.Voltaism
@Lavadalavage : Speaking of which, your code should have delete [] charPtr; rather than delete charPtr; ;-]Voltaism
@Voltaism - Really, I am signing off for the day.Lavadalavage
F
4

Yes, it's supposed to do that. You'd (primarily) notice the difference from just printing a number out directly if you do some other string-type manipulation on the result (e.g., concatenating it with other strings, searching for characters in the string).

Just for example:

std::cout << i+i;   // should print "10"
std::cout << s+s;   // should print "55"
Firebrick answered 5/4, 2011 at 17:7 Comment(0)
L
1

Besides I would prefer if I could convert the same int to char array.

char *charPtr = new char[ s.length() + 1 ] ; // s is the string in the snippet posted
strcpy( charPtr, s.c_str() ) ;

// .......

delete[] charPtr ; // Should do this, else memory leak.
Lavadalavage answered 5/4, 2011 at 17:11 Comment(6)
just wondering shouldn't the argument to strlen() be a char pointer? or would the string act the same way?Yeomanly
@Max Eastman : No, you're correct -- passing a std::string to strlen is illegal. It should be new char[s.length() + 1]; or new char[strlen(s.c_str()) + 1];, but of course the second one is inherently less efficient.Voltaism
@Voltaism - You are correct. Time for me to take rest, I guess :)Lavadalavage
@Lavadalavage : std::auto_ptr uses delete rather than delete [], so std::auto_ptr<char> would cause undefined behavior here. On C++0x compilers std::unique_ptr<char[]> would be preferred; on older compilers, boost::scoped_array or boost::shared_array are the best options.Voltaism
@Lavadalavage : Speaking of which, your code should have delete [] charPtr; rather than delete charPtr; ;-]Voltaism
@Voltaism - Really, I am signing off for the day.Lavadalavage
D
1

If you would like to stop worrying about issues like that you might be interested in boost/lexical_cast.hpp.

#include <boost/lexical_cast.hpp>
#include <string>
#include <iostream>

int main() {
  const int i=5;
  const char* s = boost::lexical_cast<std::string>(i).c_str();
  std::cout << s << std::endl;
}
Drew answered 5/4, 2011 at 18:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.