This is a C program to Convert Hexadecimal to Binary.
This program takes a hexadecimal number and converts to binary number.
1. Take a hexadecimal number as input.
2. For each bit of a hexadecimal number print its equivalent binary number in a four bit fashion. Example: For 22 print it as 0010 0010.
3. Use switch statement to access each bit of a hexadecimal number.
Here is source code of the C program to Convert Hexadecimal to Binary. The C program is successfully compiled and run on a Linux system. The program output is also shown
below.
/*
* C Program to Convert Hexadecimal to Binary
*/
#include <stdio.h>
#define MAX 1000
int main()
{
char binarynum[MAX], hexa[MAX];
long int i = 0;
printf("Enter the value for hexadecimal ");
scanf("%s", hexa);
printf("\n Equivalent binary value: ");
while (hexa[i])
{
switch (hexa[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("\n Invalid hexa digit %c ", hexa[i]);
return 0;
}
i++;
}
return 0;
}
1. Take a hexadecimal number as input and store it in the array hexa.
2. Using switch statement access each bit of the hexadecimal number and print its equivalent binary number in a four bit fashion as shown in the program.
3. Do step 2 for every bit of a input number. Use while loop to do this.
Output: Enter the value for hexadecimal ab Equivalent binary value: 10101011
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
- Apply for Computer Science Internship
- Check Computer Science Books
- Practice Computer Science MCQs
- Watch Advanced C Programming Videos
- Check C Books