w3resource

Java: Convert a binary number to hexadecimal number

Java Basic: Exercise-23 with Solution

Write a Java program to convert a binary number to a hexadecimal number.

Binary number: A binary number is a number expressed in the base-2 numeral system or binary numeral system. This system uses only two symbols: typically 0(zero) and 1(one).

Hexadecimal number: This is a positional numeral system with a radix, or base, of 16. Hexadecimal uses sixteen distinct symbols, most often the symbols 0-9 to represent values zero to nine, and A, B, C, D, E, F (or alternatively a, b, c, d, e, f) to represent values ten to fifteen.

Test Data:
Input a binary number: 1101

Pictorial Presentation: Binary to Hexadecimal number

Java: Convert a binary number to hexadecimal number

Sample Solution:

Java Code:

 import java.util.Scanner;

public class Exercise23 {
    public static void main(String[] args) {
        // Declare an array to store hexadecimal digits, variables for calculation, and binary input
        int[] hex = new int[1000];
        int i = 1, j = 0, rem, dec = 0, bin;

        // Create a Scanner object to read input from the user
        Scanner in = new Scanner(System.in);

        // Prompt the user to input a binary number
        System.out.print("Input a Binary Number: ");
        bin = in.nextInt();

        // Convert the binary number to decimal
        while (bin > 0) {
            rem = bin % 2;
            dec = dec + rem * i;
            i = i * 2;
            bin = bin / 10;
        }
        i = 0;

        // Convert the decimal number to hexadecimal
        while (dec != 0) {
            hex[i] = dec % 16;
            dec = dec / 16;
            i++;
        }

        // Display the hexadecimal value
        System.out.print("Hexadecimal value: ");
        for (j = i - 1; j >= 0; j--) {
            if (hex[j] > 9) {
                System.out.print((char)(hex[j] + 55));
            } else {
                System.out.print(hex[j]);
            }
        }
        System.out.print("\n");
    }
}

Explanation:

In the exercise above -

  • Initializes an array 'hex' to store the hexadecimal digits of the converted number, as well as other necessary variables.
  • It takes a binary number as input from the user using the "Scanner" class and stores it in the variable 'bin'.
  • It enters a loop to convert the binary number to decimal:
    • In each iteration, it calculates the remainder when the 'bin' is divided by 2 (which gives the least significant binary digit).
    • It adds this remainder multiplied by 'i' to the 'dec' variable to accumulate the decimal value.
    • It multiplies 'i' by 2 to prepare for the next binary digit.
    • It updates 'bin' by removing the least significant digit (rightmost) by dividing it by 10.
    • The loop continues until 'bin' becomes zero, effectively converting the entire binary number to decimal.
  • After converting to decimal, it enters another loop to convert the decimal number to hexadecimal:
    • In each iteration, it calculates the remainder when 'dec' is divided by 16 (which gives the least significant hexadecimal digit).
    • It stores this remainder in a 'hex' array.
    • It updates 'dec' by dividing it by 16 to move to the next hexadecimal digit.
  • Finally, it prints the hexadecimal representation of the original binary number by iterating through the 'hex' array in reverse order. It also handles hexadecimal digits greater than 9 by converting them to the corresponding letters A-F.

Sample Output:

Input a Binary Number: 1101                                                                                   
HexaDecimal value: D

Flowchart:

Flowchart: Java exercises: Convert a binary number to hexadecimal number

Java Code Editor:

Previous: Write a Java program to convert a binary number to decimal number.
Next: Write a Java program to convert a binary number to a Octal number.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/java-exercises/basic/java-basic-exercise-23.php