1

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.

user2948246
  • 75
  • 4
  • 10
  • I think you're confused. "hexidecimal" and "binary" are ways of representing values as written text. If you have a `uint32_t`, that holds a _value_, which is just a number. It is neither hexidecimal nor binary. It _looks_ like you're converting this value to an array of bits? Which are _not_ `uint8_t`? I see nothing involving hex at all. – Mooing Duck Apr 11 '15 at 00:42

2 Answers2

1

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;
Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55
0

Here some solutions!!!

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

void toBinary(uint8_t a)
{
    uint8_t i;

    for(i=0x80;i!=0;i>>=1)
        printf("%c",(a&i)?'1':'0'); 
}

void ul32toBinary(uint32_t a)
{
    uint32_t i;

    for(i=0x80000000;i!=0;i>>=1)
        printf("%c",(a&i)?'1':'0'); 
}

int main(void)
{
    uint32_t k=0xDEADBEEF;

    uint8_t *x;

    int i;

    printf("Converting 0xCC:\t");
    toBinary(0xCC); puts("");   

    printf("Converting 0xF1:\t");
    toBinary(0xF1); puts("");

    printf("Converting 0x%08X:\t",k);
    ul32toBinary(k);puts("");   

    printf("Converting 0x%08X:\t",k);
    x=(char *)&k;   
    //This is valid on little endian CPUs as Intel are!
    for(i=3;i>=0;i--) {
        toBinary(*(x+i));
        printf(" ");            
    }
    puts("");

    return 0;
}

This is the ouput of this code!

Converting 0xCC:    11001100
Converting 0xF1:    11110001
Converting 0xDEADBEEF:  11011110101011011011111011101111
Converting 0xDEADBEEF:  11011110 10101101 10111110 11101111 

To have the ouput you indicate as EXPECTED, you have to slightly modify the function toBinary() in the code above. Remember that a byte is 8 bits, not 4! To have the format you want, just insert an "if" in the right place and write a space ... an hint is: if (i==0x10) {} ... !!!

Goodnight :)

The following code is the code to print a byte in two nibbles of 4 bits:

void toBinary2(uint8_t a)
{
    uint8_t i;

    for(i=0x80;i!=0;i>>=1) {
        printf("%c",(a&i)?'1':'0');
        if (i==0x10)
            printf(" ");
    }
}
Sir Jo Black
  • 2,024
  • 2
  • 15
  • 22
  • I edited my code to get that expected output but I am now facing a new problem as mentioned in my edited post. – user2948246 Apr 11 '15 at 00:19
  • I have added at the end of my post a functions that prints byte splitted in 2 nibbles of 4 bit ... :) – Sir Jo Black Apr 11 '15 at 00:38
  • What's your goal? I understand that is to have an output in 4 bit nibbles! Your code seems to have an other direction! It's to complex if you need only to printout bits! ... :) In the code I posted above needs only that you use the function toBinary2 in the for at the end of main instead of the toBinary! – Sir Jo Black Apr 11 '15 at 00:45
  • So my main goal is to print a hexadecimal number into binary in the format mentioned below, `1101 1110 1010 1101 1011 1110 1110 1111 \n ---0 1010 1101 1011 1110 1110 1111 \n --01 1011 1110 1110 1111` But so far I've managed to print only the first line. I am not able to understand how to print the remaining lines. And in my code, convert() is just basically converting hex to binary(yes quite complicated and ugly code, sorry) and the printBits() is to print in the format. – user2948246 Apr 11 '15 at 01:07
  • If your goal is to print data in the format you say, you may easily use the code I have written. If you have problem in understanding my code ask me! (sirjoblack@hotmail.com - subj: printing-nibbles) – Sir Jo Black Apr 11 '15 at 01:18