Below is a c program which takes a hexadecimal number as an input and converts it into a binary number.

Convert Hexadecimal to Binary in C

Here is the source code of the program in C to Convert Hexadecimal number to Binary number. This program is successfully compiled, and the output is also given below.

#include<stdio.h>
int main()
{
             char hexNum[100];
	long int count=0;
	printf("Enter a hexadecimal number To Convet it into Binary : ");
	scanf("%s",hexNum);
	printf("\nBinary Number is : ");
	while(hexNum[count])
	{
		switch(hexNum[count])
		{
			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 Entry, Please Try Again  %c",hexNum[count]);
		}
		count++;
	}
	return 0;
}

Output

Convert Hexadecimal to Binary in C

Program Explanation

Take a char array hexNum of size 100 and a count variable to use it in the switch statement. Read the value of hexadecimal number form the user and save it into char array hexNum.

Use while loop to read entered value. Inside while loop, use switch cases as given in the program to execute an appropriate case and print the result on the screen.

Related Articles

Last modified: May 2, 2019