Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert from int to char 1

Status
Not open for further replies.

oueleric1

Programmer
Nov 23, 2004
2
CA
Just starting out programming. I have a book however it's not here right now and i'm in a hurry. Is it possible to change an int variable into a char varaiable? Thanks in advance.
 
If you have a single digit integer, like 6, and you want to convert it to a single character that represents that integer, like '6', then you can simply add the integer to the character '0' since the characters for the digits 0-9 are in order.
Code:
int i = 6;
char c = '0' + i;
// now c is '6'.

If you might have a multiple digit number and you want to convert it to its character representation, then you cannot store that in a character, you must store it in a string. To do this conversion you can use a stringstream.
Code:
#include <sstream>
#include <string>
// ...
int i = 372;
std::ostringstream ostr;
ostr << i;
std::string s = ostr.str();
// now s is "372".

If you have the integer value of a character and you want to convert it to the actual character, you can just assign it (with a cast).
Code:
int i = 65;
char c = static_cast<char>(i);
// now c is 'A'.
 
Some addition. The last but not least approach: use old good C (sorry, Mr.Stroustrup;) library (part of C++ Standard):
Code:
#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;
}
No need in awkward ostringstream manipulators (set then don't forget to restore etc), no awkward std::eek:stringstream words at all...
 
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 :)
 
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 :)
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?
 
i would say the easiest way is:

Code:
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 will give you a null terminated string. just make sure you b is large enough.

check out:
 
itoa() is great, unfortunately it's only on Windows. :(
 
itoa() is great, unfortunately it's only on Windows

this is the microsoft: Visual C++ forum....



If somethings hard to do, its not worth doing - Homer Simpson
 
Even so, portability is a good thing. If you can do so easily, avoiding OS-specific calls -- or at least confining them to a single file -- can make someone else's life substantially easier down the road.
 
well.. yeah.. i wasn't aware that this wasn't for a windows platform.. also.. you can't keep rewriting functions that have already been written- you will drive yourself crazy! i would say if you are developing a windows app just go ahead and use it! re-use as much code as possible!
 
Thanks alot you guys, i found an easier way around it, i kind of did it the long way the first time, however now i know. Thanks again
 
if you are developing a windows app just go ahead and use it! re-use as much code as possible!
That would work fine until your company see's that the product is selling well and decides to port it to UNIX... ;)
 
drewdaman said:
you can't keep rewriting functions that have already been written- you will drive yourself crazy!
Agreed.

But when a portable alternative already exists, and it's no more trouble to use it, you might as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top