Description
The Hexadecimal number system comprises of 16 entities. These 16 entities consist of 10 digits, 0-9 representing the first 10 numbers of the hexadecimal system and for the remaining 6 numbers, we use English alphabets ranging from A through F to represent the numbers 10 to 15.
C/C++
/* C Program to convert Binary to HexaDecimal */ //Save it as BinaryToHexaDecimal.c #include <stdio.h> #include <stdlib.h> int main(){ long int bin, hex=0, x=1, y, temp; printf("Enter binary number: "); scanf("%ld",&bin); //Storing the binary to temporary variable temp = bin; while(bin != 0) { y = bin % 10; hex = hex + y*x; x = x*2; bin = bin/10; } //Restoring the binary variable bin = temp; printf("Hexadecimal of %ld = %IX", bin, hex); return 0; }
Input: Enter a binary number : 11010 Output: Binary of 11010 is 1A
Java
/* Java Program to convert Binary to HexaDecimal */ //Save it as BinaryToHexaDecimal.java import java.io.*; import java.util.Scanner; public class BinaryToHexaDecimal { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter a binary number : "); int bin = scanner.nextInt(); String binToHexDec = binaryToHexDecimal(bin); System.out.println("Binary of " + bin + " is "+ binToHexDec); } private static String binaryToHexDecimal(int bin) { int decimalNumber = binaryToDecimal(bin); //Using the toHexString() built-in java method String hexNumber = Integer.toHexString(decimalNumber); hexNumber = hexNumber.toUpperCase(); return hexNumber; } private static int binaryToDecimal(int bin) { int rem, dec = 0; int count=0; while(bin > 0) { rem = bin % 10; dec += (rem * Math.pow(2,count)); bin/=10; count++; } return dec; } }
Input: Enter a binary number : 11010 Output: Binary of 11010 is 1A
Related Programs
1) Program to convert Binary to Decimal2) Program to convert Decimal to Octal
3) Program to convert Decimal to Binary
4) Program to convert Octal to Decimal
5) Program to find sum of digits of a number
6) Program to reverse a number
7) Program to calculate Factorial of a Number
8) Program to display multiplication table of a number
9) Program to Display Fibonacci Series
10) Program to Display Fibonacci Series using Recursion
11) Program of Simple Calculator using switch
12) Program to print all Squares of numbers from 1 to given Range
13) Program to find sum of first and last digit of a number