C program to convert Hexadecimal to Binary number

In this article, we are going to learn how to convert a Hexadecimal number to an equivalent Binary number. Here we are going to learn about two number systems one is binary and another is Hexadecimal. Hexadecimal numbers are base 16 numbers and represented in numbers from 0 to 9 and A to F. Binary numbers are base 2 numbers and represented in numbers 0 or 1 only.

Logic to convert Hexadecimal to Binary number system Hexadecimal to binary conversion is divided into three steps.

  • Extract each hex digit separately.
  • Find the binary of each extracted hex digit.
  • Store the binary equivalent of the extracted hexadecimal number in the final bin variable.
  • Repeat the above three steps till all hexadecimal digits are processed.

Hexadecimal to binary conversion

DecimalBinary Hexadecimal
000000
100011
200102
300113
401004
501015
601106
701117
810008
910019
101010A
111011B
121100C
131101D
141110E
151111F

Program to convert hexadecimal to binary number system

#include <stdio.h>
#include <string.h>

int main()
{
    char hex[17], bin[65] = "";
    int i = 0;

    /* Input hexadecimal number from user */
    printf("Enter any hexadecimal number: ");
    gets(hex);

    /* Extract first digit and find binary of each hex digit */
    for(i=0; hex[i]!='
#include <stdio.h>
#include <string.h>
int main()
{
char hex[17], bin[65] = "";
int i = 0;
/* Input hexadecimal number from user */
printf("Enter any hexadecimal number: ");
gets(hex);
/* Extract first digit and find binary of each hex digit */
for(i=0; hex[i]!='\0'; i++)
{
switch(hex[i])
{
case '0':
strcat(bin, "0000");
break;
case '1':
strcat(bin, "0001");
break;
case '2':
strcat(bin, "0010");
break;
case '3':
strcat(bin, "0011");
break;
case '4':
strcat(bin, "0100");
break;
case '5':
strcat(bin, "0101");
break;
case '6':
strcat(bin, "0110");
break;
case '7':
strcat(bin, "0111");
break;
case '8':
strcat(bin, "1000");
break;
case '9':
strcat(bin, "1001");
break;
case 'a':
case 'A':
strcat(bin, "1010");
break;
case 'b':
case 'B':
strcat(bin, "1011");
break;
case 'c':
case 'C':
strcat(bin, "1100");
break;
case 'd':
case 'D':
strcat(bin, "1101");
break;
case 'e':
case 'E':
strcat(bin, "1110");
break;
case 'f':
case 'F':
strcat(bin, "1111");
break;
default:
printf("Invalid hexadecimal input.");
}
}
printf("Hexademial number = %s\n", hex);
printf("Binary number = %s", bin);
return 0;
}
'; i++) { switch(hex[i]) { case '0': strcat(bin, "0000"); break; case '1': strcat(bin, "0001"); break; case '2': strcat(bin, "0010"); break; case '3': strcat(bin, "0011"); break; case '4': strcat(bin, "0100"); break; case '5': strcat(bin, "0101"); break; case '6': strcat(bin, "0110"); break; case '7': strcat(bin, "0111"); break; case '8': strcat(bin, "1000"); break; case '9': strcat(bin, "1001"); break; case 'a': case 'A': strcat(bin, "1010"); break; case 'b': case 'B': strcat(bin, "1011"); break; case 'c': case 'C': strcat(bin, "1100"); break; case 'd': case 'D': strcat(bin, "1101"); break; case 'e': case 'E': strcat(bin, "1110"); break; case 'f': case 'F': strcat(bin, "1111"); break; default: printf("Invalid hexadecimal input."); } } printf("Hexademial number = %s\n", hex); printf("Binary number = %s", bin); return 0; }

Output

Enter any hexadecimal number: 2f
Hexademial number = 2f
Binary number = 00101111