|
1 | 1 | package com.thealgorithms.conversions; |
2 | 2 |
|
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 | + */ |
4 | 23 | 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 | + */ |
5 | 40 | public String convert(String numHex) { |
6 | | - // String a HexaDecimal: |
7 | 41 | int conHex = Integer.parseInt(numHex, 16); |
8 | | - // Hex a Binary: |
9 | 42 | String binary = Integer.toBinaryString(conHex); |
10 | | - // Output: |
11 | 43 | return completeDigits(binary); |
12 | 44 | } |
13 | 45 |
|
| 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 | + */ |
14 | 53 | 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) { |
17 | 56 | binNum = "0" + binNum; |
18 | 57 | } |
19 | 58 | return binNum; |
20 | 59 | } |
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 | | - } |
43 | 60 | } |
0 commit comments