|
1 | 1 | package com.thealgorithms.conversions;
|
2 | 2 |
|
3 | 3 | import java.util.HashMap;
|
4 |
| -import java.util.Scanner; |
| 4 | +import java.util.Map; |
5 | 5 |
|
6 | 6 | /**
|
7 | 7 | * Converts any Binary Number to a Hexadecimal Number
|
8 | 8 | *
|
9 | 9 | * @author Nishita Aggarwal
|
10 | 10 | */
|
11 | 11 | public final class BinaryToHexadecimal {
|
| 12 | + private static final int BITS_IN_HEX_DIGIT = 4; |
| 13 | + private static final int BASE_BINARY = 2; |
| 14 | + private static final int BASE_DECIMAL = 10; |
| 15 | + private static final int HEX_START_DECIMAL = 10; |
| 16 | + private static final int HEX_END_DECIMAL = 15; |
| 17 | + |
12 | 18 | private BinaryToHexadecimal() {
|
13 | 19 | }
|
14 | 20 |
|
15 | 21 | /**
|
16 |
| - * This method converts a binary number to a hexadecimal number. |
| 22 | + * Converts a binary number to a hexadecimal number. |
17 | 23 | *
|
18 |
| - * @param binary The binary number |
19 |
| - * @return The hexadecimal number |
| 24 | + * @param binary The binary number to convert. |
| 25 | + * @return The hexadecimal representation of the binary number. |
| 26 | + * @throws IllegalArgumentException If the binary number contains digits other than 0 and 1. |
20 | 27 | */
|
21 |
| - static String binToHex(int binary) { |
22 |
| - // hm to store hexadecimal codes for binary numbers within the range: 0000 to 1111 i.e. for |
23 |
| - // decimal numbers 0 to 15 |
24 |
| - HashMap<Integer, String> hm = new HashMap<>(); |
25 |
| - // String to store hexadecimal code |
26 |
| - String hex = ""; |
27 |
| - int i; |
28 |
| - for (i = 0; i < 10; i++) { |
29 |
| - hm.put(i, String.valueOf(i)); |
30 |
| - } |
31 |
| - for (i = 10; i < 16; i++) { |
32 |
| - hm.put(i, String.valueOf((char) ('A' + i - 10))); |
33 |
| - } |
34 |
| - int currbit; |
| 28 | + public static String binToHex(int binary) { |
| 29 | + Map<Integer, String> hexMap = initializeHexMap(); |
| 30 | + StringBuilder hex = new StringBuilder(); |
| 31 | + |
35 | 32 | while (binary != 0) {
|
36 |
| - int code4 = 0; // to store decimal equivalent of number formed by 4 decimal digits |
37 |
| - for (i = 0; i < 4; i++) { |
38 |
| - currbit = binary % 10; |
39 |
| - binary = binary / 10; |
40 |
| - code4 += currbit * (int) Math.pow(2, i); |
| 33 | + int decimalValue = 0; |
| 34 | + for (int i = 0; i < BITS_IN_HEX_DIGIT; i++) { |
| 35 | + int currentBit = binary % BASE_DECIMAL; |
| 36 | + if (currentBit > 1) { |
| 37 | + throw new IllegalArgumentException("Incorrect binary digit: " + currentBit); |
| 38 | + } |
| 39 | + binary /= BASE_DECIMAL; |
| 40 | + decimalValue += (int) (currentBit * Math.pow(BASE_BINARY, i)); |
41 | 41 | }
|
42 |
| - hex = hm.get(code4) + hex; |
| 42 | + hex.insert(0, hexMap.get(decimalValue)); |
43 | 43 | }
|
44 |
| - return hex; |
| 44 | + |
| 45 | + return !hex.isEmpty() ? hex.toString() : "0"; |
45 | 46 | }
|
46 | 47 |
|
47 | 48 | /**
|
48 |
| - * Main method |
| 49 | + * Initializes the hexadecimal map with decimal to hexadecimal mappings. |
49 | 50 | *
|
50 |
| - * @param args Command line arguments |
| 51 | + * @return The initialized map containing mappings from decimal numbers to hexadecimal digits. |
51 | 52 | */
|
52 |
| - public static void main(String[] args) { |
53 |
| - Scanner sc = new Scanner(System.in); |
54 |
| - System.out.println("Enter binary number:"); |
55 |
| - int binary = sc.nextInt(); |
56 |
| - String hex = binToHex(binary); |
57 |
| - System.out.println("Hexadecimal Code:" + hex); |
58 |
| - sc.close(); |
| 53 | + private static Map<Integer, String> initializeHexMap() { |
| 54 | + Map<Integer, String> hexMap = new HashMap<>(); |
| 55 | + for (int i = 0; i < BASE_DECIMAL; i++) { |
| 56 | + hexMap.put(i, String.valueOf(i)); |
| 57 | + } |
| 58 | + for (int i = HEX_START_DECIMAL; i <= HEX_END_DECIMAL; i++) { |
| 59 | + hexMap.put(i, String.valueOf((char) ('A' + i - HEX_START_DECIMAL))); |
| 60 | + } |
| 61 | + return hexMap; |
59 | 62 | }
|
60 | 63 | }
|
0 commit comments