Enhance docs, remove main. add more tests in `HexaDecimal… (#5922)

This commit is contained in:
Hardik Pawar
2024-10-26 23:36:52 +05:30
committed by GitHub
parent cfa35a4fd9
commit ce3dd01e68
2 changed files with 81 additions and 34 deletions

View File

@ -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));
}
}
}