C program to convert hexadecimal into binary.

 

01  #include<stdio.h>
02 #define MAX 1000
03
04 int main()
05 {
06 char hexaDecimal[MAX];
07 long int i=0;
08
09 printf("Enter any hexadecimal number: ");
10 gets(hexaDecimal);
11
12 printf("nEquivalent binary value: ");
13
14 while(hexaDecimal[i])
15 {
16 switch(hexaDecimal[i])
17 {
18 case '0': printf("0000"); break;
19 case '1': printf("0001"); break;
20 case '2': printf("0010"); break;
21 case '3': printf("0011"); break;
22 case '4': printf("0100"); break;
23 case '5': printf("0101"); break;
24 case '6': printf("0110"); break;
25 case '7': printf("0111"); break;
26 case '8': printf("1000"); break;
27 case '9': printf("1001"); break;
28
29 case 'A':
30 case 'a': printf("1010"); break;
31
32 case 'B':
33 case 'b': printf("1011"); break;
34
35 case 'C':
36 case 'c': printf("1100"); break;
37
38 case 'D':
39 case 'd': printf("1101"); break;
40
41 case 'E':
42 case 'e': printf("1110"); break;
43
44 case 'F':
45 case 'f': printf("1111"); break;
46
47 default: printf("nInvalid hexadecimal digit %c ",hexaDecimal[i]);
48 return;
49
50 }
51 i++; // loop increment
52 }
53 return 0;
54 }
 
 
OUTPUT :

Enter any hexadecimal number : 1AC
Equivalent binary value : 000110101100

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *