Skip to content

Commit c20375a

Browse files
authored
refactor: BinaryToHexadecimal (#5331)
1 parent ec30592 commit c20375a

File tree

2 files changed

+50
-39
lines changed

2 files changed

+50
-39
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,63 @@
11
package com.thealgorithms.conversions;
22

33
import java.util.HashMap;
4-
import java.util.Scanner;
4+
import java.util.Map;
55

66
/**
77
* Converts any Binary Number to a Hexadecimal Number
88
*
99
* @author Nishita Aggarwal
1010
*/
1111
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+
1218
private BinaryToHexadecimal() {
1319
}
1420

1521
/**
16-
* This method converts a binary number to a hexadecimal number.
22+
* Converts a binary number to a hexadecimal number.
1723
*
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.
2027
*/
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+
3532
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));
4141
}
42-
hex = hm.get(code4) + hex;
42+
hex.insert(0, hexMap.get(decimalValue));
4343
}
44-
return hex;
44+
45+
return !hex.isEmpty() ? hex.toString() : "0";
4546
}
4647

4748
/**
48-
* Main method
49+
* Initializes the hexadecimal map with decimal to hexadecimal mappings.
4950
*
50-
* @param args Command line arguments
51+
* @return The initialized map containing mappings from decimal numbers to hexadecimal digits.
5152
*/
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;
5962
}
6063
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
package com.thealgorithms.conversions;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45

5-
import org.junit.jupiter.api.Test;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.CsvSource;
68

79
public class BinaryToHexadecimalTest {
810

9-
@Test
10-
public void testBinaryToHexadecimal() {
11-
assertEquals("6A", BinaryToHexadecimal.binToHex(1101010));
12-
assertEquals("C", BinaryToHexadecimal.binToHex(1100));
11+
@ParameterizedTest
12+
@CsvSource({"0, 0", "1, 1", "10, 2", "1111, F", "1101010, 6A", "1100, C"})
13+
void testBinToHex(int binary, String expectedHex) {
14+
assertEquals(expectedHex, BinaryToHexadecimal.binToHex(binary));
15+
}
16+
17+
@ParameterizedTest
18+
@CsvSource({"2", "1234", "11112"})
19+
void testInvalidBinaryInput(int binary) {
20+
assertThrows(IllegalArgumentException.class, () -> BinaryToHexadecimal.binToHex(binary));
1321
}
1422
}

0 commit comments

Comments
 (0)