c - Convert uint8_t hex value to binary -


so says in title trying convert hexadecimal binary. problem have been facing that, given value uint32_t type. far, have convert uint32_t uint8_t such each of uint8_t variables hold 8 bits of original hex value. have succesfully converted hexadecimal binary, in code shown below. unable print in format.

    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 come idea           }      }      int main( int argc, const char * argv[] )      {             uint32_t value = 0xdeadbeefl;             bool     bits[32];             int len;                           (  len = 32; len > 0; len -= 7 )             {                  convert (value, bits, len );                  printbits (bits, len);             }             return 0;      } 

even though think main idea on every iteration len out of 32 bits must converted binary , printed, not able incorporate idea code. decided convert entire hex number , try print needed.

you're shifting far 1 thing...i going 8..1 should go 7..0 (and i should be, loop counters, int. if unsigned have won't ever < 0 stop loop). also, you're unpacking 32-bit value wrong:

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

Popular posts from this blog