How to convert int to char?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • andrewanderson
    New Member
    • Jan 2007
    • 24

    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?
    Code:
    #include <iostream.h>
    
    int main()
    {
    	int num;
    	char conv;
    
    	cout<<"Enter the number range between 0-9"<<endl;
    	cin>>num;
    	switch(num)
    	{
    	case 1:
    		{
    			conv = 01;
    			num = conv;
    			break;
    		}
    	case 2:
    	case 3:
    	case 4:
    	case 5:
    		{
    			cout<<"number";
    			break;
    		}
    	default:
    		{
    			cout<<"number";
    			break;
    		}
    	}
    	cout<<num;
    	return 0;
    }
    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
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    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.

    Comment

    • shinichi81
      New Member
      • Mar 2007
      • 7

      #3
      Basically, you can use sprintf() to convert integer, float, handle...into a string.

      Comment

      • andrewanderson
        New Member
        • Jan 2007
        • 24

        #4
        Originally posted by Ganon11
        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.

        Comment

        • andrewanderson
          New Member
          • Jan 2007
          • 24

          #5
          Originally posted by shinichi81
          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?

          Comment

          • horace1
            Recognized Expert Top Contributor
            • Nov 2006
            • 1510

            #6
            Originally posted by andrewanderson
            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.a sp
            http://www.zeuscmd.com/tutorials/cplusplus/43-StringNumberCon versions.php

            Comment

            • Extremist
              New Member
              • Jan 2007
              • 94

              #7
              Funny you should ask how to use sprintf

              Like this:
              Code:
              int something;
              int from = 9;
              char tobe;
              something = sprintf(tobe,"%d",&from);
              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

              Comment

              • horace1
                Recognized Expert Top Contributor
                • Nov 2006
                • 1510

                #8
                Originally posted by Extremist
                Funny you should ask how to use sprintf

                Like this:
                Code:
                int something;
                int from = 9;
                char tobe;
                something = sprintf(tobe,"%d",&from);
                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.
                Code:
                int from = 9;
                char tobe[10];
                something = sprintf(tobe,"%d",from);
                tobe will then contain '9' plus '\0' terminator

                Comment

                • Extremist
                  New Member
                  • Jan 2007
                  • 94

                  #9
                  Originally posted by horace1
                  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.
                  Code:
                  int from = 9;
                  char tobe[10];
                  something = sprintf(tobe,"%d",from);
                  tobe will then contain '9' plus '\0' terminator
                  Ok, I forgot the *, thanks for the correction

                  Comment

                  • lqdeffx
                    New Member
                    • Mar 2007
                    • 39

                    #10
                    just wanted to throw my 2 cents in *throws*
                    another way, the c++ way.
                    Code:
                    #include <string>
                    #include <sstream>
                    
                    inline std::string intToStr(unsigned int x) {
                    	std::ostringstream o;
                    	if (!(o << x))
                    		throw RunTimeError("Converting integer to string failed!");
                    	return o.str();
                    }

                    Comment

                    Working...