Skip to content

Commit ce3dd01

Browse files
authored
Enhance docs, remove main. add more tests in `HexaDecimal… (#5922)
1 parent cfa35a4 commit ce3dd01

File tree

2 files changed

+81
-34
lines changed

2 files changed

+81
-34
lines changed
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 byteSize = 8;
55+
while (binNum.length() < byteSize) {
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

+36-6
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,44 @@
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

8+
/**
9+
* Unit tests for the {@link EndianConverter} class.
10+
*/
711
public class HexaDecimalToBinaryTest {
812

9-
@Test
10-
public void testHexaDecimalToBinary() {
11-
HexaDecimalToBinary hexaDecimalToBinary = new HexaDecimalToBinary();
12-
assertEquals("1111111111111111111111111111111", hexaDecimalToBinary.convert("7fffffff"));
13-
assertEquals("101010111100110111101111", hexaDecimalToBinary.convert("abcdef"));
13+
/**
14+
* Parameterized test to validate the conversion from little-endian to big-endian.
15+
* Hexadecimal values are passed as strings and converted to integers during the test.
16+
*/
17+
@ParameterizedTest
18+
@CsvSource({
19+
"0x78563412, 0x12345678", "0x00000000, 0x00000000", "0x00000001, 0x01000000",
20+
"0xFFFFFFFF, 0xFFFFFFFF", // -1 in two's complement
21+
"0x0000007F, 0x7F000000" // Positive boundary case
22+
})
23+
public void
24+
testLittleToBigEndian(String inputHex, String expectedHex) {
25+
int input = (int) Long.parseLong(inputHex.substring(2), 16); // Convert hex string to int
26+
int expected = (int) Long.parseLong(expectedHex.substring(2), 16); // Convert hex string to int
27+
assertEquals(expected, EndianConverter.littleToBigEndian(input));
28+
}
29+
30+
/**
31+
* Parameterized test to validate the conversion from big-endian to little-endian.
32+
*/
33+
@ParameterizedTest
34+
@CsvSource({
35+
"0x12345678, 0x78563412", "0x00000000, 0x00000000", "0x01000000, 0x00000001",
36+
"0xFFFFFFFF, 0xFFFFFFFF", // -1 in two's complement
37+
"0x7F000000, 0x0000007F" // Positive boundary case
38+
})
39+
public void
40+
testBigToLittleEndian(String inputHex, String expectedHex) {
41+
int input = (int) Long.parseLong(inputHex.substring(2), 16); // Convert hex string to int
42+
int expected = (int) Long.parseLong(expectedHex.substring(2), 16); // Convert hex string to int
43+
assertEquals(expected, EndianConverter.bigToLittleEndian(input));
1444
}
1545
}

0 commit comments

Comments
 (0)