From dbdfd9654d6d5349b4ddb5497c6659d479043069 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Mon, 21 Oct 2024 11:38:15 +0530 Subject: [PATCH 1/4] refactor: Enhance docs, remove `main`. add more tests in `HexaDecimalToBinary` --- .../conversions/HexaDecimalToBinary.java | 73 ++++++++++++------- .../conversions/HexaDecimalToBinaryTest.java | 30 ++++++-- 2 files changed, 69 insertions(+), 34 deletions(-) diff --git a/src/main/java/com/thealgorithms/conversions/HexaDecimalToBinary.java b/src/main/java/com/thealgorithms/conversions/HexaDecimalToBinary.java index b6228488dc76..8ef70ce09ade 100644 --- a/src/main/java/com/thealgorithms/conversions/HexaDecimalToBinary.java +++ b/src/main/java/com/thealgorithms/conversions/HexaDecimalToBinary.java @@ -1,43 +1,60 @@ package com.thealgorithms.conversions; -// Hex [0-9],[A-F] -> Binary [0,1] +/** + * Utility class for converting hexadecimal numbers to binary representation. + *

+ * A hexadecimal number consists of digits from {@code [0-9]} and {@code [A-F]} (case-insensitive), + * while binary representation uses only {@code [0, 1]}. + *

+ * This class provides methods to: + *

+ *

+ * Example: + *

+ * + *

This class assumes that the input hexadecimal string is valid.

+ */ public class HexaDecimalToBinary { + + /** + * Converts a hexadecimal string to its binary string equivalent. + * The binary output is padded to a minimum of 8 bits (1 byte). + * Steps: + *
    + *
  1. Convert the hexadecimal string to an integer.
  2. + *
  3. Convert the integer to a binary string.
  4. + *
  5. Pad the binary string to ensure it is at least 8 bits long.
  6. + *
  7. Return the padded binary string.
  8. + *
+ * + * @param numHex the hexadecimal string (e.g., "A1", "7F") + * @throws NumberFormatException if the input string is not a valid hexadecimal number + * @return the binary string representation, padded to 8 bits (e.g., "10100001") + */ public String convert(String numHex) { - // String a HexaDecimal: int conHex = Integer.parseInt(numHex, 16); - // Hex a Binary: String binary = Integer.toBinaryString(conHex); - // Output: return completeDigits(binary); } + /** + * Pads the binary string to ensure it is at least 8 bits long. + * If the binary string is shorter than 8 bits, it adds leading zeros. + * + * @param binNum the binary string to pad + * @return the padded binary string with a minimum length of 8 + */ public String completeDigits(String binNum) { - final int longBits = 8; - for (int i = binNum.length(); i < longBits; i++) { + final int BYTE_SIZE = 8; + while (binNum.length() < BYTE_SIZE) { binNum = "0" + binNum; } return binNum; } - - public static void main(String[] args) { - // Testing Numbers: - String[] hexNums = { - "1", - "A1", - "ef", - "BA", - "AA", - "BB", - "19", - "01", - "02", - "03", - "04", - }; - HexaDecimalToBinary objConvert = new HexaDecimalToBinary(); - - for (String num : hexNums) { - System.out.println(num + " = " + objConvert.convert(num)); - } - } } diff --git a/src/test/java/com/thealgorithms/conversions/HexaDecimalToBinaryTest.java b/src/test/java/com/thealgorithms/conversions/HexaDecimalToBinaryTest.java index 72a0a0174a93..24f3f39f8824 100644 --- a/src/test/java/com/thealgorithms/conversions/HexaDecimalToBinaryTest.java +++ b/src/test/java/com/thealgorithms/conversions/HexaDecimalToBinaryTest.java @@ -2,14 +2,32 @@ import static org.junit.jupiter.api.Assertions.assertEquals; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; public class HexaDecimalToBinaryTest { - @Test - public void testHexaDecimalToBinary() { - HexaDecimalToBinary hexaDecimalToBinary = new HexaDecimalToBinary(); - assertEquals("1111111111111111111111111111111", hexaDecimalToBinary.convert("7fffffff")); - assertEquals("101010111100110111101111", hexaDecimalToBinary.convert("abcdef")); + private final HexaDecimalToBinary converter = new HexaDecimalToBinary(); + + @ParameterizedTest + @CsvSource({"1, 00000001", "A1, 10100001", "EF, 11101111", "BA, 10111010", "7, 00000111", "F, 00001111", "7FFFFFFF, 1111111111111111111111111111111", "ABCDEF, 101010111100110111101111"}) + public void testHexaDecimalToBinary(String hexInput, String expectedBinary) { + assertEquals(expectedBinary, converter.convert(hexInput)); + } + + @ParameterizedTest + @CsvSource({"0, 00000000", "2, 00000010", "3, 00000011"}) + public void testPaddingWithLeadingZeros(String hexInput, String expectedBinary) { + assertEquals(expectedBinary, converter.convert(hexInput)); + } + + @ParameterizedTest + @CsvSource({"G, NumberFormatException", "ZZ, NumberFormatException"}) + public void testInvalidHexInput(String hexInput, String expectedException) { + try { + converter.convert(hexInput); + } catch (NumberFormatException e) { + assertEquals(expectedException, e.getClass().getSimpleName()); + } } } From c2ce5270d1ce3784c0d5cefe52d672fc9e61ffcf Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Mon, 21 Oct 2024 11:40:49 +0530 Subject: [PATCH 2/4] Fix --- .../conversions/HexaDecimalToBinaryTest.java | 48 ++++++++++++------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/src/test/java/com/thealgorithms/conversions/HexaDecimalToBinaryTest.java b/src/test/java/com/thealgorithms/conversions/HexaDecimalToBinaryTest.java index 24f3f39f8824..1426eab64d2c 100644 --- a/src/test/java/com/thealgorithms/conversions/HexaDecimalToBinaryTest.java +++ b/src/test/java/com/thealgorithms/conversions/HexaDecimalToBinaryTest.java @@ -5,29 +5,41 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; +/** + * Unit tests for the {@link EndianConverter} class. + */ public class HexaDecimalToBinaryTest { - private final HexaDecimalToBinary converter = new HexaDecimalToBinary(); - - @ParameterizedTest - @CsvSource({"1, 00000001", "A1, 10100001", "EF, 11101111", "BA, 10111010", "7, 00000111", "F, 00001111", "7FFFFFFF, 1111111111111111111111111111111", "ABCDEF, 101010111100110111101111"}) - public void testHexaDecimalToBinary(String hexInput, String expectedBinary) { - assertEquals(expectedBinary, converter.convert(hexInput)); - } - + /** + * Parameterized test to validate the conversion from little-endian to big-endian. + * Hexadecimal values are passed as strings and converted to integers during the test. + */ @ParameterizedTest - @CsvSource({"0, 00000000", "2, 00000010", "3, 00000011"}) - public void testPaddingWithLeadingZeros(String hexInput, String expectedBinary) { - assertEquals(expectedBinary, converter.convert(hexInput)); + @CsvSource({ + "0x78563412, 0x12345678", "0x00000000, 0x00000000", "0x00000001, 0x01000000", + "0xFFFFFFFF, 0xFFFFFFFF", // -1 in two's complement + "0x0000007F, 0x7F000000" // Positive boundary case + }) + public void + testLittleToBigEndian(String inputHex, String expectedHex) { + int input = (int) Long.parseLong(inputHex.substring(2), 16); // Convert hex string to int + int expected = (int) Long.parseLong(expectedHex.substring(2), 16); // Convert hex string to int + assertEquals(expected, EndianConverter.littleToBigEndian(input)); } + /** + * Parameterized test to validate the conversion from big-endian to little-endian. + */ @ParameterizedTest - @CsvSource({"G, NumberFormatException", "ZZ, NumberFormatException"}) - public void testInvalidHexInput(String hexInput, String expectedException) { - try { - converter.convert(hexInput); - } catch (NumberFormatException e) { - assertEquals(expectedException, e.getClass().getSimpleName()); - } + @CsvSource({ + "0x12345678, 0x78563412", "0x00000000, 0x00000000", "0x01000000, 0x00000001", + "0xFFFFFFFF, 0xFFFFFFFF", // -1 in two's complement + "0x7F000000, 0x0000007F" // Positive boundary case + }) + public void + testBigToLittleEndian(String inputHex, String expectedHex) { + int input = (int) Long.parseLong(inputHex.substring(2), 16); // Convert hex string to int + int expected = (int) Long.parseLong(expectedHex.substring(2), 16); // Convert hex string to int + assertEquals(expected, EndianConverter.bigToLittleEndian(input)); } } From efa5305fbe55aef25028fd0bfc09615ad404b586 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Mon, 21 Oct 2024 11:42:33 +0530 Subject: [PATCH 3/4] Fix --- .../com/thealgorithms/conversions/HexaDecimalToBinary.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/conversions/HexaDecimalToBinary.java b/src/main/java/com/thealgorithms/conversions/HexaDecimalToBinary.java index 8ef70ce09ade..73432c57f863 100644 --- a/src/main/java/com/thealgorithms/conversions/HexaDecimalToBinary.java +++ b/src/main/java/com/thealgorithms/conversions/HexaDecimalToBinary.java @@ -51,8 +51,8 @@ public String convert(String numHex) { * @return the padded binary string with a minimum length of 8 */ public String completeDigits(String binNum) { - final int BYTE_SIZE = 8; - while (binNum.length() < BYTE_SIZE) { + final int BYTESIZE = 8; + while (binNum.length() < BYTESIZE) { binNum = "0" + binNum; } return binNum; From a827a5af14ece1d3de405090fdee61e861cb8e82 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Mon, 21 Oct 2024 11:46:06 +0530 Subject: [PATCH 4/4] Fix --- .../com/thealgorithms/conversions/HexaDecimalToBinary.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/conversions/HexaDecimalToBinary.java b/src/main/java/com/thealgorithms/conversions/HexaDecimalToBinary.java index 73432c57f863..07acefc9fb14 100644 --- a/src/main/java/com/thealgorithms/conversions/HexaDecimalToBinary.java +++ b/src/main/java/com/thealgorithms/conversions/HexaDecimalToBinary.java @@ -51,8 +51,8 @@ public String convert(String numHex) { * @return the padded binary string with a minimum length of 8 */ public String completeDigits(String binNum) { - final int BYTESIZE = 8; - while (binNum.length() < BYTESIZE) { + final int byteSize = 8; + while (binNum.length() < byteSize) { binNum = "0" + binNum; } return binNum;