Convert uint8_t hex value to binary

user2948246 picture user2948246 · Apr 9, 2015 · Viewed 7.6k times · Source

So like it says in the title I am trying to convert a hexadecimal into a binary. But the problem I have been facing is that, the given value is an uint32_t type. So far, I have convert from uint32_t to uint8_t such each of the uint8_t variables hold 8 bits of the original hex value. I have succesfully converted the hexadecimal into binary, in the code shown below. But now i am unable to print it in a certain format.

    The Format is:
    1101 1110 1010 1101 1011 1110 1110 1111
    ---0 1010 1101 1011 1110 1110 1111
    --01 1011 1110 1110 1111


    void convert(uint32_t value,bool bits[],int length)
    {
          uint8_t a0,a1,a2,a3;
          int i,c,k,j;
          a0 = value;
          a1 = value >> 8;
          a2 = value >> 16;
          a3 = value >> 24;
          for(i=0,c=7;i<=7,c>=0;i++,c--)
          {
                k = a3>>c;
                if(k&1)
                   bits[i] = true;
                else
                   bits[i] = false;
          }
          for(i=8,c=7;i<=15,c>=0;i++,c--)
          {
               k = a2>>c;
               if(k&1)
                  bits[i] = true;
               else
                  bits[i] = false;
           } 
          for(i=16,c=7;i<=23,c>=0;i++,c--)
          {
                k = a1>>c;
               if(k&1)
                   bits[i] = true;
               else
                   bits[i] = false;
          }
          for(i=24,c=7;i<=31,c>=0;i++,c--)
          {
                k = a0>>c;
                if(k&1)
                    bits[i] = true;
                else
                    bits[i] = false;
          }
          for(i=0;i<32;i=i+4)
          {
               for(j=i;j<i+4;j++)
               {
                    printf("%d",bits[j]);
               }
               printf(" ");
           }
     }
     void printBits(bool *bits,int length)
     {
          int len =32,i,j;
          int y = len-length-3;
          printf("\n");
          for(i=0,j=y;i<32,j<32;i++,j++)
          {
            // Trying to come up with some idea
          }
     }
     int main( int argc, const char * argv[] )
     {
            uint32_t value = 0xDEADBEEFL;
            bool     bits[32];
            int len;              
            for (  len = 32; len > 0; len -= 7 )
            {
                 convert (value, bits, len );
                 printBits (bits, len);
            }
            return 0;
     }

Even though I think the main idea is that on every iteration len out of 32 bits must be converted into binary and printed, I was not able to incorporate that idea into my code. So I decided to convert the entire hex number and try to print only what is needed.

Answer

Lee Daniel Crocker picture Lee Daniel Crocker · Apr 9, 2015

You're shifting too far for one thing...i is going 8..1 where it should go 7..0 (and i should be, like all loop counters, an int. If its unsigned like you have it you won't ever be < 0 to stop the loop). Also, you're unpacking the 32-bit value wrong:

a0 = (value >> 24) & 0xFF;
a1 = (value >> 16) & 0xFF;
a2 = (value >> 8) & 0xFF;
a3 = value & 0xFF;