473,320 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to convert int to char?

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
9 5523
Ganon11
3,652 Expert 2GB
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
Basically, you can use sprintf() to convert integer, float, handle...into a string.
Mar 7 '07 #3
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
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
horace1
1,510 Expert 1GB
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
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
horace1
1,510 Expert 1GB
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
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
lqdeffx
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

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

Similar topics

5
by: Brad Moore | last post by:
Hey all, I'm getting the following compiler error from my code. I was wondering if anyone could help me understand the concept behind it (I actually did try and compile this degenerate...
7
by: MilanB | last post by:
Hello How to convert char to int? Thanks
2
by: Alper Akcayoz | last post by:
Hello Esteemed Developpers I would like to thank you in advance for your sincere responses I am a fresh Visual C++ .NET Developer. Can you kindly guide me for How to Convert char* to System::String
1
by: Elioth | last post by:
Hi... I need to know how to convert Char Array to Byte Array and vice-versa in VB 2K5 Thanks for all help. Elioth
2
by: Joah Senegal | last post by:
Hello I need to convert a chat to a string... but I don't know how to do this. i;ve searched the internet but I've only find some code to convert char * to string or char to string.... but I...
3
by: simon | last post by:
I am a fresh Visual C++ .NET Developer. Can you kindly guide me for How to Convert char to System::String I am using windows forms and trying to set a text value referencing the method...
2
by: Rasheed | last post by:
Hi, i have a char pointer buffer which will hold the bitmap buffer, So i need to convert char *buffer as a bitmap. becuase when i draw on the Dialog using char *buffer. Nothing will be dispalyed....
1
by: helios | last post by:
Hi all, I'm resolved problem. and I want anybody need me that convert char to array bits char ConvertChar2ArrayBit(char ch) { char Bits; .... return Bits; } for...
3
by: mamul | last post by:
Hi please some one can help me. how to convert char * to string? i have take char *argv from command line and want to pass to a function as string object(string str) i want to first convert argv...
3
by: DaveRook | last post by:
Hi I've had a few error messages with this I don't understand why. It works fine in a Windows Form, but now moving it to a website, it's not working. I don't have the option to count the lines as...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.