codescracker
c

C Program to Convert Binary to Hexadecimal



« Previous Program Next Program »

In this tutorial, we will learn about how to create a program in C that can convert any given binary number by user (at run-time) into its equivalent hexadecimal value. At last we have also created same purpose function-driven program that also converts binary number to hexadecimal number.

But before going through the program, if you are not aware of

then refer to Binary to Hexadecimal conversion step by step process. Now let's move on to the program.

Binary to Hexadecimal in C

The question is, Write a program in C that converts binary number to hexadecimal. The answer to this question is:

#include<stdio.h>
#include<conio.h>
int main()
{
    int binnum, hex=0, mul=1, count=1, rem, i=0;
    char hexnum[20];
    printf("Enter any Binary Number: ");
    scanf("%d", &binnum);
    while(binnum!=0)
    {
        rem = binnum%10;
        hex = hex + (rem*mul);
        if(count%4==0)
        {
            if(hex<10)
                hexnum[i] = hex+48;
            else
                hexnum[i] = hex+55;
            mul = 1;
            hex = 0;
            count = 1;
            i++;
        }
        else
        {
            mul = mul*2;
            count++;
        }
        binnum = binnum/10;
    }
    if(count!=1)
        hexnum[i] = hex+48;
    if(count==1)
        i--;
    printf("\nEquivalent Hexadecimal Value = ");
    for(i=i; i>=0; i--)
        printf("%c", hexnum[i]);
    getch();
    return 0;
}

As the above program was written under Code::Blocks IDE, therefore after successful build and run, you will get the following output on your output screen. This is the first snapshot of the sample run:

c program to convert binary to hexadecimal

Now supply any binary number say 1101110 and press ENTER key to see its equivalent hexadecimal value as shown in the second snapshot of the sample run given below:

c binary to hexadecimal

Here is the final snapshot of above program's another sample run:

binary to hexadecimal conversion in c

Program Explained

Binary to Hexadecimal in C using User-defined Function

Here is another program using user-defined function named BinToHex() that also does the same job, that is to convert any binary number to hexadecimal number.

Here, we have declared the variable i and the character array hexnum[] as global to make it known in both the function, that is main() and BinToHex().

#include<stdio.h>
#include<conio.h>
void BinToHex(int bin);
int i=0;
char hexnum[20];
int main()
{
    int binnum;
    printf("Enter any Binary Number: ");
    scanf("%d", &binnum);
    BinToHex(binnum);
    printf("\nEquivalent Hexadecimal Value = ");
    for(i=i; i>=0; i--)
        printf("%c", hexnum[i]);
    getch();
    return 0;
}
void BinToHex(int bin)
{
    int hex=0, mul=1, count=1, rem;
    while(bin!=0)
    {
        rem = bin%10;
        hex = hex + (rem*mul);
        if(count%4==0)
        {
            if(hex<10)
                hexnum[i] = hex+48;
            else
                hexnum[i] = hex+55;
            mul = 1;
            hex = 0;
            count = 1;
            i++;
        }
        else
        {
            mul = mul*2;
            count++;
        }
        bin = bin/10;
    }
    if(count!=1)
        hexnum[i] = hex+48;
    if(count==1)
        i--;
}

Here is the final snapshot of the above program's sample run:

c binary to hexadecimal using function

Same Program in Other Languages

C Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker