The input string hex D2B9CB The input string bin 1101 0010 1
Solution
#include <stdio.h>
#include <string.h>
// BINARY function that prints binary equivalent
// hexadecimal value is passed as a parameter
void binary(char hex[50])
{
int i=0;
while(i<strlen(hex)) // as long as characters are in hex
{
// checking whether char is 0
if(hex[i]==\'0\')
printf(\"0000 \"); // printing equivalent binary with space at end
else if(hex[i]==\'1\')
printf(\"0001 \");
else if(hex[i]==\'2\')
printf(\"0010 \");
else if(hex[i]==\'3\')
printf(\"0011 \");
else if(hex[i]==\'4\')
printf(\"0100 \");
else if(hex[i]==\'5\')
printf(\"0101 \");
else if(hex[i]==\'6\')
printf(\"0110 \");
else if(hex[i]==\'7\')
printf(\"0111 \");
else if(hex[i]==\'8\')
printf(\"1000 \");
else if(hex[i]==\'9\')
printf(\"1001 \");
else if(hex[i]==\'A\')
printf(\"1010 \");
else if(hex[i]==\'B\')
printf(\"1011 \");
else if(hex[i]==\'C\')
printf(\"1100 \");
else if(hex[i]==\'D\')
printf(\"1101 \");
else if(hex[i]==\'E\')
printf(\"1110 \");
else if(hex[i]==\'F\')
printf(\"1111 \");
i++;
}
}
// main function
int main(int arg, char *argv[])
{
// calling function
// argv[1] contains the HEX value
// checking whether the string if length inbetween 3 and 40
if(strlen(argv[1])>=3 && strlen(argv[1])<41)
binary(argv[1]);
else
printf(\"Length of hex value is invalid\");
return 0;
}
/*
Input : ./a.out D2B9CBF
Output : 1101 0010 1011 1001 1100 1011 1111
*/

