By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,303 Members | 1,595 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,303 IT Pros & Developers. It's quick & easy.

How to convert int to char?

P: 24
hi all members

i would like to know how to convert int to char? can any tell me?
this is what i did is it rite?
Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2.  
  3. int main()
  4. {
  5.     int num;
  6.     char conv;
  7.  
  8.     cout<<"Enter the number range between 0-9"<<endl;
  9.     cin>>num;
  10.     switch(num)
  11.     {
  12.     case 1:
  13.         {
  14.             conv = 01;
  15.             num = conv;
  16.             break;
  17.         }
  18.     case 2:
  19.     case 3:
  20.     case 4:
  21.     case 5:
  22.         {
  23.             cout<<"number";
  24.             break;
  25.         }
  26.     default:
  27.         {
  28.             cout<<"number";
  29.             break;
  30.         }
  31.     }
  32.     cout<<num;
  33.     return 0;
  34. }
basically im trying to print 0 infront of the number if it is a single digit.
example if i input 1 it will print 01 on the console window. is this the rite way of doing it? or is there another way of doin it?

thanks for replying and helping me!!cheers~~

regrads andrew
Mar 7 '07 #1
Share this Question
Share on Google+
9 Replies


Ganon11
Expert 2.5K+
P: 3,652
Well, you can test to see if the number is lower than 10 (and, thus, has one digit). If it is, then print a 0 out, then the number. If not, just print the number.
Mar 7 '07 #2

P: 7
Basically, you can use sprintf() to convert integer, float, handle...into a string.
Mar 7 '07 #3

P: 24
Well, you can test to see if the number is lower than 10 (and, thus, has one digit). If it is, then print a 0 out, then the number. If not, just print the number.
yea i understand wat u mean ganon by using cout to print the 0 infront but besides using cout i would like to rename it have the number of 01 which char can print out but int can't.
Mar 7 '07 #4

P: 24
Basically, you can use sprintf() to convert integer, float, handle...into a string.
hmmm wondering how to use sprintf()? i'm kinda new to c++!!got any website to see or can any1 tell me how?
Mar 7 '07 #5

Expert 100+
P: 1,510
hmmm wondering how to use sprintf()? i'm kinda new to c++!!got any website to see or can any1 tell me how?
sprintf() can be used in C to convert numeric data to character arrays. However, in C++ you can use stringstream to convert numeric data to strings, e.g.
http://www.codeproject.com/vcpp/stl/ostringstream.asp
http://www.zeuscmd.com/tutorials/cplusplus/43-StringNumberConversions.php
Mar 7 '07 #6

P: 94
Funny you should ask how to use sprintf

Like this:
Expand|Select|Wrap|Line Numbers
  1.  
  2. int something;
  3. int from = 9;
  4. char tobe;
  5. something = sprintf(tobe,"%d",&from);
  6.  
  7.  
You clearly tried, so let me explain.
sprintf return the amount of characters converted, the from is the number to be converted.
And the tobe is the char which gets the value of from as a char
Mar 7 '07 #7

Expert 100+
P: 1,510
Funny you should ask how to use sprintf

Like this:
Expand|Select|Wrap|Line Numbers
  1.  
  2. int something;
  3. int from = 9;
  4. char tobe;
  5. something = sprintf(tobe,"%d",&from);
  6.  
  7.  
You clearly tried, so let me explain.
sprintf return the amount of characters converted, the from is the number to be converted.
And the tobe is the char which gets the value of from as a char
tobe should be a char* (pointer to sufficent chars to take the converted data) and you don't need the & in front of from (which will print a pointer value), e.g.
Expand|Select|Wrap|Line Numbers
  1. int from = 9;
  2. char tobe[10];
  3. something = sprintf(tobe,"%d",from);
  4.  
tobe will then contain '9' plus '\0' terminator
Mar 7 '07 #8

P: 94
tobe should be a char* (pointer to sufficent chars to take the converted data) and you don't need the & in front of from (which will print a pointer value), e.g.
Expand|Select|Wrap|Line Numbers
  1. int from = 9;
  2. char tobe[10];
  3. something = sprintf(tobe,"%d",from);
  4.  
tobe will then contain '9' plus '\0' terminator
Ok, I forgot the *, thanks for the correction
Mar 7 '07 #9

P: 39
just wanted to throw my 2 cents in *throws*
another way, the c++ way.
Expand|Select|Wrap|Line Numbers
  1. #include <string>
  2. #include <sstream>
  3.  
  4. inline std::string intToStr(unsigned int x) {
  5.     std::ostringstream o;
  6.     if (!(o << x))
  7.         throw RunTimeError("Converting integer to string failed!");
  8.     return o.str();
  9. }
  10.  
Mar 7 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.