Binary To Hexadecimal Algorithm

The Binary to Hexadecimal Algorithm is a widely used technique for converting binary numbers, which are represented in base 2, into their equivalent hexadecimal representation, which is in base 16. This algorithm is particularly useful in various computer programming and electronic applications, as it provides a more compact and human-readable way to represent long binary strings. Hexadecimal uses 16 unique symbols to represent numbers, which include the digits 0-9 for values 0-9 and the letters A-F for values 10-15. This higher base system allows a single hexadecimal digit to represent four binary digits (also known as bits), making it an efficient way to express binary data. To perform the Binary to Hexadecimal Algorithm, the input binary number is first divided into groups of four bits, starting from the least significant bit (LSB) to the most significant bit (MSB). If the input binary number does not have enough bits to form complete groups of four, leading zeros can be added to fill the missing places. Next, each group of four bits is converted into its corresponding hexadecimal digit by comparing it to a reference table or using simple arithmetic operations. For example, the binary group 1010 would be converted to the hexadecimal digit 'A', since its decimal equivalent is 10. Finally, the converted hexadecimal digits are combined in the same order as the original binary groups, resulting in the final hexadecimal representation of the input binary number.
package Conversions;

import java.util.*;

/**
 * Converts any Binary Number to a Hexadecimal Number
 *
 * @author Nishita Aggarwal
 */
public class BinaryToHexadecimal {

    /**
     * This method converts a binary number to
     * a hexadecimal number.
     *
     * @param binary The binary number
     * @return The hexadecimal number
     */
    static String binToHex(int binary) {
        //hm to store hexadecimal codes for binary numbers within the range: 0000 to 1111 i.e. for decimal numbers 0 to 15
        HashMap<Integer, String> hm = new HashMap<>();
        //String to store hexadecimal code
        String hex = "";
        int i;
        for (i = 0; i < 10; i++) {
            hm.put(i, String.valueOf(i));
        }
        for (i = 10; i < 16; i++) hm.put(i, String.valueOf((char) ('A' + i - 10)));
        int currbit;
        while (binary != 0) {
            int code4 = 0;    //to store decimal equivalent of number formed by 4 decimal digits
            for (i = 0; i < 4; i++) {
                currbit = binary % 10;
                binary = binary / 10;
                code4 += currbit * Math.pow(2, i);
            }
            hex = hm.get(code4) + hex;
        }
        return hex;
    }

    /**
     * Main method
     *
     * @param args Command line arguments
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter binary number:");
        int binary = sc.nextInt();
        String hex = binToHex(binary);
        System.out.println("Hexadecimal Code:" + hex);
        sc.close();
    }
}

LANGUAGE:

DARK MODE: