Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. I would like to convert int to char

I would like to convert int to char

Scheduled Pinned Locked Moved C / C++ / MFC
question
15 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S sacoskun

    Hi, I would like to convert int i = 7 to char, how can I do this? Regards, - When in doubt, push a pawn! -

    S Offline
    S Offline
    Sujan Christo
    wrote on last edited by
    #3

    Hi, can use _itoa(..) function. Have a look @ msdn. it does have a sample too. Sujan

    1 Reply Last reply
    0
    • M Mad__

      Hy, That depend on what you need, char = 0x07 or char = '7' ;)

      S Offline
      S Offline
      sacoskun
      wrote on last edited by
      #4

      char = '7' - When in doubt, push a pawn! -

      M T 2 Replies Last reply
      0
      • S sacoskun

        char = '7' - When in doubt, push a pawn! -

        M Offline
        M Offline
        Mad__
        wrote on last edited by
        #5

        You can use on of : sprintf, itoa ... of this pseudo code int = nex_digit while(int) char = 0x30+int

        S 1 Reply Last reply
        0
        • M Mad__

          You can use on of : sprintf, itoa ... of this pseudo code int = nex_digit while(int) char = 0x30+int

          S Offline
          S Offline
          sacoskun
          wrote on last edited by
          #6

          I would like to run this function in my C++ code; void convertToBinary( int numberToConvert ) { if(!(( N / 2 ) < 1 )) { binaryRepresentation[counter] = ( N % 2 ); counter++; convertToBinary( N /= 2 ); } } but binaryRepresentation[counter] = ( N % 2 ); line of code does not work friend? - When in doubt, push a pawn! -

          M D 2 Replies Last reply
          0
          • S sacoskun

            char = '7' - When in doubt, push a pawn! -

            T Offline
            T Offline
            toxcct
            wrote on last edited by
            #7

            char c;
            sscanf("7", "%s %c", &c);


            TOXCCT >>> GEII power

            S 1 Reply Last reply
            0
            • T toxcct

              char c;
              sscanf("7", "%s %c", &c);


              TOXCCT >>> GEII power

              S Offline
              S Offline
              sacoskun
              wrote on last edited by
              #8

              When I made sscanf( static_cast( N % 2 ) , "%s" , &binaryRepresentation[counter] ); Compiler gives an error like cannot convert parameter 1 from 'char' to 'const char * I'm a newbie in C++, Regards. - When in doubt, push a pawn! -

              T B 2 Replies Last reply
              0
              • S sacoskun

                I would like to run this function in my C++ code; void convertToBinary( int numberToConvert ) { if(!(( N / 2 ) < 1 )) { binaryRepresentation[counter] = ( N % 2 ); counter++; convertToBinary( N /= 2 ); } } but binaryRepresentation[counter] = ( N % 2 ); line of code does not work friend? - When in doubt, push a pawn! -

                M Offline
                M Offline
                Mad__
                wrote on last edited by
                #9

                I don't understend what meen N in this code ;) For binare representation of value i use this function: char* BinPrintf(int value) { char tmp[256]; memset(tmp, 0, 256); int tpos = 0; while(value) { tmp[tpos++] = 0x30+value%2; value = value >> 1; } char *ret = new char[strlen(tmp)+1]; memset(ret, 0, strlen(tmp)+1); tpos = strlen(tmp)-1; value = 0; while(tpos > -1) ret[value++] = tmp[tpos--]; return ret; }

                D 1 Reply Last reply
                0
                • S sacoskun

                  When I made sscanf( static_cast( N % 2 ) , "%s" , &binaryRepresentation[counter] ); Compiler gives an error like cannot convert parameter 1 from 'char' to 'const char * I'm a newbie in C++, Regards. - When in doubt, push a pawn! -

                  T Offline
                  T Offline
                  toxcct
                  wrote on last edited by
                  #10

                  kromozom wrote: sscanf( static_cast( N % 2 ) , "%s" , &binaryRepresentation[counter] ); that's normal, you associate a char with %s do this :

                  sscanf( static_cast( N % 2 ) , "%c" , &binaryRepresentation[counter] );


                  TOXCCT >>> GEII power

                  S 1 Reply Last reply
                  0
                  • T toxcct

                    kromozom wrote: sscanf( static_cast( N % 2 ) , "%s" , &binaryRepresentation[counter] ); that's normal, you associate a char with %s do this :

                    sscanf( static_cast( N % 2 ) , "%c" , &binaryRepresentation[counter] );


                    TOXCCT >>> GEII power

                    S Offline
                    S Offline
                    sacoskun
                    wrote on last edited by
                    #11

                    When I type; sscanf( static_cast( N % 2 ) , "%c" , &binaryRepresentation[counter] ); Same error occurs, cannot convert char to const char* Regards, - When in doubt, push a pawn! -

                    1 Reply Last reply
                    0
                    • S sacoskun

                      When I made sscanf( static_cast( N % 2 ) , "%s" , &binaryRepresentation[counter] ); Compiler gives an error like cannot convert parameter 1 from 'char' to 'const char * I'm a newbie in C++, Regards. - When in doubt, push a pawn! -

                      B Offline
                      B Offline
                      Bob Stanneveld
                      wrote on last edited by
                      #12

                      That is because static_cast( N % 2 ) is not a pointer to a char. I don't know what it is, because N is unknown to me... Take a look at the prototype of sscanf[^] and then start wondering how you should pass the parameters... Besides that, the solution that Mad__ gave you looks like what you want... :-D Multiply it by infinity and take it beyond eternity and you'll still have no idea about what I'm talking about.

                      1 Reply Last reply
                      0
                      • S sacoskun

                        I would like to run this function in my C++ code; void convertToBinary( int numberToConvert ) { if(!(( N / 2 ) < 1 )) { binaryRepresentation[counter] = ( N % 2 ); counter++; convertToBinary( N /= 2 ); } } but binaryRepresentation[counter] = ( N % 2 ); line of code does not work friend? - When in doubt, push a pawn! -

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #13

                        I assume binaryRepresentation is some sort of char array, and that N is actually numberToConvert. That being the case, what you have is working, but you are confusing 0 (ASCII 0) and '0' (ASCII 48). You simply need to change the statement to:

                        binaryRepresentation[counter] = (char) ((N % 2 ) + 48);

                        Now binaryRepresentation[counter] will contain '0' if N is an even number and '1' if N is an odd number. Of course, if you wanted to use a "built-in" solution, you could simply use itoa(numberToConvert, binaryRepresentation, 2).


                        "Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow

                        S 1 Reply Last reply
                        0
                        • M Mad__

                          I don't understend what meen N in this code ;) For binare representation of value i use this function: char* BinPrintf(int value) { char tmp[256]; memset(tmp, 0, 256); int tpos = 0; while(value) { tmp[tpos++] = 0x30+value%2; value = value >> 1; } char *ret = new char[strlen(tmp)+1]; memset(ret, 0, strlen(tmp)+1); tpos = strlen(tmp)-1; value = 0; while(tpos > -1) ret[value++] = tmp[tpos--]; return ret; }

                          D Offline
                          D Offline
                          David Crow
                          wrote on last edited by
                          #14

                          Mad__ wrote: char *ret = new char[strlen(tmp)+1]; memset(ret, 0, strlen(tmp)+1); tpos = strlen(tmp)-1; value = 0; while(tpos > -1) ret[value++] = tmp[tpos--]; This code could simply be replaced with a call to strrev(tmp).


                          "Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow

                          1 Reply Last reply
                          0
                          • D David Crow

                            I assume binaryRepresentation is some sort of char array, and that N is actually numberToConvert. That being the case, what you have is working, but you are confusing 0 (ASCII 0) and '0' (ASCII 48). You simply need to change the statement to:

                            binaryRepresentation[counter] = (char) ((N % 2 ) + 48);

                            Now binaryRepresentation[counter] will contain '0' if N is an even number and '1' if N is an odd number. Of course, if you wanted to use a "built-in" solution, you could simply use itoa(numberToConvert, binaryRepresentation, 2).


                            "Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow

                            S Offline
                            S Offline
                            sacoskun
                            wrote on last edited by
                            #15

                            Thanks all for help, I solved it... - When in doubt, push a pawn! -

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups