Convert Binary to Hexadecimal in C

In this source code example, we will write a code to binary value to Hexadecimal value in the C programming language.

Convert Binary to Hexadecimal in C

In this C program, we will take input from the User or console. 
/*
 * C Program to Convert Binary to Hexadecimal
 */

#include <stdio.h>

int main()
{
    int binary;
    long hexa = 0, i = 1, remainder;

    printf("Enter the binary number: ");
    scanf("%d", &binary);
    while (binary != 0)
    {
        remainder = binary % 10;
        hexa = hexa + remainder * i;
        i = i * 2;
        binary = binary / 10;
    }
    printf("The equivalent hexadecimal value: %lX", hexa);
    return 0;
}

Output:

Enter the binary number: 1110
The equivalent hexadecimal value: E

Demo

Convert Binary to Hexadecimal in C

Related Conversions


Comments