Hexadecimal to binary conversion in c





C program for hexadecimal to binary conversion

#include<stdio.h>
#define MAX 1000

int main(){
    char binaryNumber[MAX],hexaDecimal[MAX];
    long int i=0;

    printf("Enter any hexadecimal number: ");
    scanf("%s",hexaDecimal);

    printf("\nEquivalent binary value: ");
    while(hexaDecimal[i]){
         switch(hexaDecimal[i]){
             case '0': printf("0000"); break;
             case '1': printf("0001"); break;
             case '2': printf("0010"); break;
             case '3': printf("0011"); break;
             case '4': printf("0100"); break;
             case '5': printf("0101"); break;
             case '6': printf("0110"); break;
             case '7': printf("0111"); break;
             case '8': printf("1000"); break;
             case '9': printf("1001"); break;
             case 'A': printf("1010"); break;
             case 'B': printf("1011"); break;
             case 'C': printf("1100"); break;
             case 'D': printf("1101"); break;
             case 'E': printf("1110"); break;
             case 'F': printf("1111"); break;
             case 'a': printf("1010"); break;
             case 'b': printf("1011"); break;
             case 'c': printf("1100"); break;
             case 'd': printf("1101"); break;
             case 'e': printf("1110"); break;
             case 'f': printf("1111"); break;
             default:  printf("\nInvalid hexadecimal digit %c ",hexaDecimal[i]); return 0;
         }
         i++;
    }

    return 0;
}

Sample output:

Enter any hexadecimal number: 2AD5
Equivalent binary value: 0010101011010101


Algorithm:

Hexadecimal to binary conversion method:

To convert or change the hexadecimal number to binary number replace the each octal digits by a binary number using hexadecimal to binary chart.

Hexadecimal
Binary
0
0000
1
0001
2
0010
3
0011
4
0100
5
0101
6
0110
7
0111
8
1000
9
1001
A
1010
B
1011
C
1100
D
1101
E
1110
F
1111
Hexadecimal to binary table

Hexadecimal to binary conversion examples:

For example we want to convert or change hexadecimal number 65B2 to binary. For this we will replace each hexadecimal digit to binary values using the above table:

Hexadecimal number:  6    5    B    2
Binary values:     0110 0101 1011 0010

So (65B2)16 = (0110010110110010)2




6. Write a c program to convert octal number to hexadecimal number.
8. Write a c program to convert hexadecimal number to octal number.
9. Write a c program to convert hexadecimal number to decimal number.
10. Write a c program to convert binary number to octal number.

3 comments:

Unknown said...

why do you use char to store the binary value

kamisetty tejyapranthi said...

#include
#include
int main()
{
int n,m[100],i=0,j=0,x=0,l[100],k;
printf("enter octal number:");
scanf("%d",&n);
printf("\n");
while(n>0)
{

m[i]=n%10;
x+=m[i]*pow(8,i);
n=n/10;
i++;
}
printf("decimal equivalent is : %d\n",x);
printf("HEXA DECIMAL equivalent is:");
while(x>0)
{
l[j++]=x%16;
//printf("%d ",l[j++]);
x=x/16;

}
for(k=j-1;k>=0;k--)
{

if(l[k]==10)
printf("A ");
else if(l[k]==11)
printf("B ");
else if(l[k]==12)
printf("C ");
else if(l[k]==13)
printf("D ");
else if(l[k]==14)
printf("E ");
else if(l[k]==15)
printf("F ");
else
printf("%d ",l[k]);

}
}

kamisetty tejyapranthi said...

#include
#include
#include
/**hexa decimal to octal conversion**/

int main()
{
char h[100];
int i = 0, val, len;
int dec = 0;
int place = 1;
int j=0,m[100];
printf("Enter any hexadecimal number: ");
gets(h);
len = strlen(h);
len--;
for(i=0; h[i]!='\0'; i++)
{
switch(h[i])
{
case '0':
val = 0;
break;
case '1':
val = 1;
break;
case '2':
val = 2;
break;
case '3':
val = 3;
break;
case '4':
val = 4;
break;
case '5':
val = 5;
break;
case '6':
val = 6;
break;
case '7':
val = 7;
break;
case '8':
val = 8;
break;
case '9':
val = 9;
break;
case 'a':
case 'A':
val = 10;
break;
case 'b':
case 'B':
val = 11;
break;
case 'c':
case 'C':
val = 12;
break;
case 'd':
case 'D':
val = 13;
break;
case 'e':
case 'E':
val = 14;
break;
case 'f':
case 'F':
val = 15;
break;
}

dec+= val * pow(16, len);
len--;
}

printf("\nHexadecimal number = %s\n", h);
printf("Decimal number = %lld", dec);
printf("\noctal equivalent of hexa decimal is:");
while(dec>0)
{
m[j++]=dec%8;
dec=dec/8;
}
for(i=j-1;i>=0;i--)
{
printf("%d ",m[i]);
}

}