package cn.iocoder.yudao.framework.common.thealgorithms.conversions;

import java.util.HashMap;
import java.util.Scanner;

/**
 * 将任何二进制数转换为十六进制数
 *
 * @author 西田·阿加瓦尔
 */
public final class BinaryToHexadecimal {
    private BinaryToHexadecimal() {
    }

   /**
     * 此方法将二进制数转换为十六进制数。
     *
     * @param binary 二进制数
     * @return 十六进制数
     */
    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 * (int) 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();
    }
}
