Reputation: 77
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.
Upvotes: 1
Views: 10388
Reputation: 2096
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(" ");
}
}
Upvotes: 0
Reputation: 13171
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;
Upvotes: 1