C Program to Convert Binary to Hexadecimal

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

But before going through the program, if you are not aware of how the binary to hexadecimal conversion takes place, refer to the step-by-step process of binary to hexadecimal conversion. Now let's move on to the program.

Binary to Hexadecimal in C

The question is, "Write a program in C that converts a 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;
}

Because the above program was written in the Code::Blocks IDE, you will see the following output on your output screen after a successful build and run. 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 the 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 the above program's another sample run:

binary to hexadecimal conversion in c

Program Explained

Binary to Hexadecimal in C using a User-Defined Function

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

Here, we have declared the variable i and the character array hexnum[] as global to make them known in both the functions, 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

The same program in different languages

C Quiz


« Previous Program Next Program »


Liked this post? Share it!