C Program For Hexadecimal To Binary Conversion
Let us learn how to write a code for hexadecimal to binary conversion in C programming language. This C program to convert hexadecimal number into binary value makes use of a switch case. The switch method includes a case for every digit of the hexadecimal number system.
Hexadecimal Number System
The hexadecimal number system is also known as Hex. The base of a hexadecimal number is 16. Therefore, it includes a total of 16 symbols including numbers and characters.
The symbols acceptable in a hexadecimal number system are as follows:
A, B, C, D, E, F, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0
where, A = 10, B = 11, C = 12, D = 13, E = 14, F = 15.
Example
(F12E)16 = (1111 0001 0010 1110)2
Every character or number in a hexadecimal number system has a particular value in binary number system. All you need is to match that value with the binary number format.
Hexadecimal To Binary Conversion C Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #include<stdio.h> int main() { long int count = 0; char hexadecimal_value[85], binary_value[85]; printf("\nEnter Hexa - Decimal Value:\t"); scanf("%s", hexadecimal_value); printf("\nBinary Value of the Hexadecimal Number:\t"); while(hexadecimal_value[count]) { switch(hexadecimal_value[count]) { 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; 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; default: printf("nEntered Value [%c] is Invalid", hexadecimal_value[count]); } count++; } printf("\n"); return 0; } |
Output

If you have any doubt or compilation error in this C code for hexadecimal to binary conversion, let us know about it in the comment section.
Such a simple Hex to Decimal Number conversion c program. Switch cases makes it so easy to solve it. Thanks.
You’re welcome. Actually Hexadecimal to Binary conversion programs are quite simple to understand if done using switch cases.