Convert Hexadecimal to Binary in C

Advertisements

Prev Tutorial Next Tutorial

Convert Hexadecimal to Binary Program in C

In this types of program we takes a Hexadecimal number as input and converts it into binary number. To convert Hexadecimal to binary number first accept binary as a input then convert into equivalent Hexadecimal number

  • Take a binary number as input.
  • Make Pair of 4 digits binary number.
  • Convert these three binary pair number into equivalent Decimal number.
  • Combind these decimal number.
  • Done

Programming Steps to Convert Hexadecimal to Binary

  • Take a Hexadecimal number as input.
  • Print the binary value of each digit of a Hexadecimal number. Use switch statement and while loop to do this.

Convert Hexadecimal to Binary in C

#include<stdio.h>
#include<conio.h>
#define MAX 1000

void main()
{
char binary_number[MAX], hexa[MAX];
    long int i = 0;
	clrscr();
 
    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++;
    }
	getch();
}

Output 1

Please Enter any Hexadecimal Number: E1
Equivalent Binary Value 11100001

Output 2

Please Enter any Hexadecimal Number: D2
Equivalent Binary Value 11010010

Prev Tutorial Next Tutorial

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements