Skip to content

Commit dbdfd96

Browse files
committed
refactor: Enhance docs, remove main. add more tests in HexaDecimalToBinary
1 parent e499d3b commit dbdfd96

File tree

2 files changed

+69
-34
lines changed

2 files changed

+69
-34
lines changed
Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,60 @@
11
package com.thealgorithms.conversions;
22

3-
// Hex [0-9],[A-F] -> Binary [0,1]
3+
/**
4+
* Utility class for converting hexadecimal numbers to binary representation.
5+
* <p>
6+
* A hexadecimal number consists of digits from {@code [0-9]} and {@code [A-F]} (case-insensitive),
7+
* while binary representation uses only {@code [0, 1]}.
8+
* <p>
9+
* This class provides methods to:
10+
* <ul>
11+
* <li>Convert a hexadecimal string to its binary string equivalent.</li>
12+
* <li>Ensure the binary output is padded to 8 bits (1 byte).</li>
13+
* </ul>
14+
* <p>
15+
* Example:
16+
* <ul>
17+
* <li>{@code "A1"} → {@code "10100001"}</li>
18+
* <li>{@code "1"} → {@code "00000001"}</li>
19+
* </ul>
20+
*
21+
* <p>This class assumes that the input hexadecimal string is valid.</p>
22+
*/
423
public class HexaDecimalToBinary {
24+
25+
/**
26+
* Converts a hexadecimal string to its binary string equivalent.
27+
* The binary output is padded to a minimum of 8 bits (1 byte).
28+
* Steps:
29+
* <ol>
30+
* <li>Convert the hexadecimal string to an integer.</li>
31+
* <li>Convert the integer to a binary string.</li>
32+
* <li>Pad the binary string to ensure it is at least 8 bits long.</li>
33+
* <li>Return the padded binary string.</li>
34+
* </ol>
35+
*
36+
* @param numHex the hexadecimal string (e.g., "A1", "7F")
37+
* @throws NumberFormatException if the input string is not a valid hexadecimal number
38+
* @return the binary string representation, padded to 8 bits (e.g., "10100001")
39+
*/
540
public String convert(String numHex) {
6-
// String a HexaDecimal:
741
int conHex = Integer.parseInt(numHex, 16);
8-
// Hex a Binary:
942
String binary = Integer.toBinaryString(conHex);
10-
// Output:
1143
return completeDigits(binary);
1244
}
1345

46+
/**
47+
* Pads the binary string to ensure it is at least 8 bits long.
48+
* If the binary string is shorter than 8 bits, it adds leading zeros.
49+
*
50+
* @param binNum the binary string to pad
51+
* @return the padded binary string with a minimum length of 8
52+
*/
1453
public String completeDigits(String binNum) {
15-
final int longBits = 8;
16-
for (int i = binNum.length(); i < longBits; i++) {
54+
final int BYTE_SIZE = 8;
55+
while (binNum.length() < BYTE_SIZE) {
1756
binNum = "0" + binNum;
1857
}
1958
return binNum;
2059
}
21-
22-
public static void main(String[] args) {
23-
// Testing Numbers:
24-
String[] hexNums = {
25-
"1",
26-
"A1",
27-
"ef",
28-
"BA",
29-
"AA",
30-
"BB",
31-
"19",
32-
"01",
33-
"02",
34-
"03",
35-
"04",
36-
};
37-
HexaDecimalToBinary objConvert = new HexaDecimalToBinary();
38-
39-
for (String num : hexNums) {
40-
System.out.println(num + " = " + objConvert.convert(num));
41-
}
42-
}
4360
}

src/test/java/com/thealgorithms/conversions/HexaDecimalToBinaryTest.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,32 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

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

78
public class HexaDecimalToBinaryTest {
89

9-
@Test
10-
public void testHexaDecimalToBinary() {
11-
HexaDecimalToBinary hexaDecimalToBinary = new HexaDecimalToBinary();
12-
assertEquals("1111111111111111111111111111111", hexaDecimalToBinary.convert("7fffffff"));
13-
assertEquals("101010111100110111101111", hexaDecimalToBinary.convert("abcdef"));
10+
private final HexaDecimalToBinary converter = new HexaDecimalToBinary();
11+
12+
@ParameterizedTest
13+
@CsvSource({"1, 00000001", "A1, 10100001", "EF, 11101111", "BA, 10111010", "7, 00000111", "F, 00001111", "7FFFFFFF, 1111111111111111111111111111111", "ABCDEF, 101010111100110111101111"})
14+
public void testHexaDecimalToBinary(String hexInput, String expectedBinary) {
15+
assertEquals(expectedBinary, converter.convert(hexInput));
16+
}
17+
18+
@ParameterizedTest
19+
@CsvSource({"0, 00000000", "2, 00000010", "3, 00000011"})
20+
public void testPaddingWithLeadingZeros(String hexInput, String expectedBinary) {
21+
assertEquals(expectedBinary, converter.convert(hexInput));
22+
}
23+
24+
@ParameterizedTest
25+
@CsvSource({"G, NumberFormatException", "ZZ, NumberFormatException"})
26+
public void testInvalidHexInput(String hexInput, String expectedException) {
27+
try {
28+
converter.convert(hexInput);
29+
} catch (NumberFormatException e) {
30+
assertEquals(expectedException, e.getClass().getSimpleName());
31+
}
1432
}
1533
}

0 commit comments

Comments
 (0)