Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
int i = 6;
char c = '0' + i;
// now c is '6'.
#include <sstream>
#include <string>
// ...
int i = 372;
std::ostringstream ostr;
ostr << i;
std::string s = ostr.str();
// now s is "372".
int i = 65;
char c = static_cast<char>(i);
// now c is 'A'.
#include <cstdio>
...
int i = 372;
string std::s;
...
{ // block if you need local buffer...
char ibuff[24]; // or what else, or use outer char array
sprintf(ibuff,"%d",i); // use a proper format: "%04d"...
s = ibuff;
}
Could you be more specific about what is wrong with the examples given. Other than the comment in my third example assuming an ASCII character set, I don't see any problems. Also, what's a library conversion routine for converting an int to a char?tmiketx said:never convert numbers using knowledge of the ordinal positions of the numeral inside the character set. it would be like upshifting letters by adding & subtracing the biases into the ASCII uppercase/lowercase sequence. use the library conversion routines instead, as they work for all characters sets; the majority of the examples given in response to your question should never be used in production code - and certainly not at performance-review time
int a = 27;
char * b= new char [50]; //you will have to think about
//the size yourself
itoa(a, b, 10); //b will now contain the value you want
//as a string
itoa() is great, unfortunately it's only on Windows
That would work fine until your company see's that the product is selling well and decides to port it to UNIX...if you are developing a windows app just go ahead and use it! re-use as much code as possible!
Agreed.drewdaman said:you can't keep rewriting functions that have already been written- you will drive yourself crazy!