Skip to content

refactor: Enhance docs, remove main. add more tests in `HexaDecimal… #5922

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* A hexadecimal number consists of digits from {@code [0-9]} and {@code [A-F]} (case-insensitive),
* while binary representation uses only {@code [0, 1]}.
* <p>
* This class provides methods to:
* <ul>
* <li>Convert a hexadecimal string to its binary string equivalent.</li>
* <li>Ensure the binary output is padded to 8 bits (1 byte).</li>
* </ul>
* <p>
* Example:
* <ul>
* <li>{@code "A1"} → {@code "10100001"}</li>
* <li>{@code "1"} → {@code "00000001"}</li>
* </ul>
*
* <p>This class assumes that the input hexadecimal string is valid.</p>
*/
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:
* <ol>
* <li>Convert the hexadecimal string to an integer.</li>
* <li>Convert the integer to a binary string.</li>
* <li>Pad the binary string to ensure it is at least 8 bits long.</li>
* <li>Return the padded binary string.</li>
* </ol>
*
* @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 byteSize = 8;
while (binNum.length() < byteSize) {
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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,44 @@

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;

/**
* Unit tests for the {@link EndianConverter} class.
*/
public class HexaDecimalToBinaryTest {

@Test
public void testHexaDecimalToBinary() {
HexaDecimalToBinary hexaDecimalToBinary = new HexaDecimalToBinary();
assertEquals("1111111111111111111111111111111", hexaDecimalToBinary.convert("7fffffff"));
assertEquals("101010111100110111101111", hexaDecimalToBinary.convert("abcdef"));
/**
* 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({
"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({
"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));
}
}