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. Convert decimal to binary in C

Convert decimal to binary in C

Scheduled Pinned Locked Moved C / C++ / MFC
help
8 Posts 4 Posters 14 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 Offline
    S Offline
    sahil Ranka
    wrote on last edited by
    #1

    I'm attempting to convert a decimal to binary such as 192 to 11000000. I simply need a basic code to do this yet the code I have so far doesn't work:

    void dectobin(int value, char* output)
    {
    int i;
    output[5] = '\0';
    for (i = 4; i >= 0; --i, value >>= 1)
    {
    output[i] = (value & 1) + '0';
    }
    }

    Prior to this, done thorough research on decimal to binary in cI and gone through some good Articles, to have a clear understanding of the topic. Any help would be much appreciated!

    Mircea NeacsuM L CPalliniC 4 Replies Last reply
    0
    • S sahil Ranka

      I'm attempting to convert a decimal to binary such as 192 to 11000000. I simply need a basic code to do this yet the code I have so far doesn't work:

      void dectobin(int value, char* output)
      {
      int i;
      output[5] = '\0';
      for (i = 4; i >= 0; --i, value >>= 1)
      {
      output[i] = (value & 1) + '0';
      }
      }

      Prior to this, done thorough research on decimal to binary in cI and gone through some good Articles, to have a clear understanding of the topic. Any help would be much appreciated!

      Mircea NeacsuM Offline
      Mircea NeacsuM Offline
      Mircea Neacsu
      wrote on last edited by
      #2

      You don’t say exactly why it doesn’t work so I’ll just guess: the code works only with numbers up to 2^5=32. If you tried it with 192 it would have failed. As a general advice, for small algorithms like that, it helps to “play the computer”: take a piece of paper and go through each step as you would be the computer. That gives you a better understanding of how the algorithm works and helps you find eventual bugs.

      Mircea

      1 Reply Last reply
      0
      • S sahil Ranka

        I'm attempting to convert a decimal to binary such as 192 to 11000000. I simply need a basic code to do this yet the code I have so far doesn't work:

        void dectobin(int value, char* output)
        {
        int i;
        output[5] = '\0';
        for (i = 4; i >= 0; --i, value >>= 1)
        {
        output[i] = (value & 1) + '0';
        }
        }

        Prior to this, done thorough research on decimal to binary in cI and gone through some good Articles, to have a clear understanding of the topic. Any help would be much appreciated!

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Your code is correct as far as the conversion goes. The problem is that it can not handle any binary value greater than 31. You need to calculate the number of digits in the converted value first, and create an array big enough to store the string.

        S 1 Reply Last reply
        0
        • S sahil Ranka

          I'm attempting to convert a decimal to binary such as 192 to 11000000. I simply need a basic code to do this yet the code I have so far doesn't work:

          void dectobin(int value, char* output)
          {
          int i;
          output[5] = '\0';
          for (i = 4; i >= 0; --i, value >>= 1)
          {
          output[i] = (value & 1) + '0';
          }
          }

          Prior to this, done thorough research on decimal to binary in cI and gone through some good Articles, to have a clear understanding of the topic. Any help would be much appreciated!

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          As noted in other answers, your code handles at most four bits. Moreover, your function is assuming a (at least) 5-bytes buffer is provided by the caller. That's a flawn: the caller should provide the size of the buffer and the called should check if the size is big enough to produce the requested output. Try

          #include
          #include
          #include

          bool uint2binstr(unsigned value, char output[], size_t output_size)
          {
          unsigned v = value;
          unsigned bits = 0;
          while ( v )
          {
          v >>= 1;
          ++bits;
          }
          if ( ! bits ) ++bits;

          if ( output_size > bits )
          {
          output[bits] = '\0';
          while (bits)
          {
          --bits;
          output[bits] = (value & 1) + '0';
          value >>= 1;
          }
          return true;
          }
          return false;
          }

          enum { N = 33 }; // assuming 'unsigned' is 32 bits

          int main()
          {
          char out[N];

          unsigned a[] = { 0, 1, 128, 192, 65535, 65536, -1 };

          for (size_t n=0; n
          "In testa che avete, Signor di Ceprano?"
          -- Rigoletto

          In testa che avete, signor di Ceprano?

          S 1 Reply Last reply
          0
          • CPalliniC CPallini

            As noted in other answers, your code handles at most four bits. Moreover, your function is assuming a (at least) 5-bytes buffer is provided by the caller. That's a flawn: the caller should provide the size of the buffer and the called should check if the size is big enough to produce the requested output. Try

            #include
            #include
            #include

            bool uint2binstr(unsigned value, char output[], size_t output_size)
            {
            unsigned v = value;
            unsigned bits = 0;
            while ( v )
            {
            v >>= 1;
            ++bits;
            }
            if ( ! bits ) ++bits;

            if ( output_size > bits )
            {
            output[bits] = '\0';
            while (bits)
            {
            --bits;
            output[bits] = (value & 1) + '0';
            value >>= 1;
            }
            return true;
            }
            return false;
            }

            enum { N = 33 }; // assuming 'unsigned' is 32 bits

            int main()
            {
            char out[N];

            unsigned a[] = { 0, 1, 128, 192, 65535, 65536, -1 };

            for (size_t n=0; n
            "In testa che avete, Signor di Ceprano?"
            -- Rigoletto

            S Offline
            S Offline
            sahil Ranka
            wrote on last edited by
            #5

            Well explained. Thank You!

            CPalliniC 1 Reply Last reply
            0
            • L Lost User

              Your code is correct as far as the conversion goes. The problem is that it can not handle any binary value greater than 31. You need to calculate the number of digits in the converted value first, and create an array big enough to store the string.

              S Offline
              S Offline
              sahil Ranka
              wrote on last edited by
              #6

              Thanks Richard MacCutchan I will Try this.

              1 Reply Last reply
              0
              • S sahil Ranka

                Well explained. Thank You!

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #7

                You are welcome.

                "In testa che avete, Signor di Ceprano?" -- Rigoletto

                In testa che avete, signor di Ceprano?

                1 Reply Last reply
                0
                • S sahil Ranka

                  I'm attempting to convert a decimal to binary such as 192 to 11000000. I simply need a basic code to do this yet the code I have so far doesn't work:

                  void dectobin(int value, char* output)
                  {
                  int i;
                  output[5] = '\0';
                  for (i = 4; i >= 0; --i, value >>= 1)
                  {
                  output[i] = (value & 1) + '0';
                  }
                  }

                  Prior to this, done thorough research on decimal to binary in cI and gone through some good Articles, to have a clear understanding of the topic. Any help would be much appreciated!

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  You have updated your question, but it is not clear what has changed since yesterday when we gave you the answer.

                  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