Repository: TheAlgorithms/Java
Pull request #5337 (2024-08-16 22:09:42)

refactor: `DecimalToHexadecimal`
Rename the previous file.
Improving the code to make it more readable.
Adding additional tests to this algorithm.

<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5337?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 42.88%. Comparing base [(`7c58b19`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/7c58b190c8ebd375cb87f873e0acfdc21282d7e8?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`0fde3bf`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/0fde3bfa70d4d2914a189b39c0e4fc724b8353ce?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff            @@
##             master    #5337   +/-   ##
=========================================
  Coverage     42.87%   42.88%           
  Complexity     2661     2661           
=========================================
  Files           522      522           
  Lines         15483    15475    -8     
  Branches       2950     2950           
=========================================
- Hits           6638     6636    -2     
+ Misses         8560     8554    -6     
  Partials        285      285           
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5337?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/src/main/java/com/thealgorithms/conversions/DecimalToHexaDecimal.java b/src/main/java/com/thealgorithms/conversions/DecimalToHexaDecimal.java
deleted file mode 100644
index 78838c61..00000000
--- a/src/main/java/com/thealgorithms/conversions/DecimalToHexaDecimal.java
+++ /dev/null
@@ -1,51 +0,0 @@
-package com.thealgorithms.conversions;
-
-// hex = [0 - 9] -> [A - F]
-final class DecimalToHexaDecimal {
-    private DecimalToHexaDecimal() {
-    }
-
-    private static final int SIZE_OF_INT_IN_HALF_BYTES = 8;
-    private static final int NUMBER_OF_BITS_IN_HALF_BYTE = 4;
-    private static final int HALF_BYTE = 0x0F;
-    private static final char[] HEX_DIGITS = {
-        '0',
-        '1',
-        '2',
-        '3',
-        '4',
-        '5',
-        '6',
-        '7',
-        '8',
-        '9',
-        'A',
-        'B',
-        'C',
-        'D',
-        'E',
-        'F',
-    };
-
-    // Returns the hex value of the dec entered in the parameter.
-    public static String decToHex(int dec) {
-        StringBuilder hexBuilder = new StringBuilder(SIZE_OF_INT_IN_HALF_BYTES);
-        hexBuilder.setLength(SIZE_OF_INT_IN_HALF_BYTES);
-        for (int i = SIZE_OF_INT_IN_HALF_BYTES - 1; i >= 0; --i) {
-            int j = dec & HALF_BYTE;
-            hexBuilder.setCharAt(i, HEX_DIGITS[j]);
-            dec >>= NUMBER_OF_BITS_IN_HALF_BYTE;
-        }
-        return hexBuilder.toString().toLowerCase();
-    }
-
-    // Test above function.
-    public static void main(String[] args) {
-        System.out.println("Test...");
-        int dec = 305445566;
-        String libraryDecToHex = Integer.toHexString(dec);
-        String decToHex = decToHex(dec);
-        System.out.println("Result from the library : " + libraryDecToHex);
-        System.out.println("Result decToHex method : " + decToHex);
-    }
-}
diff --git a/src/main/java/com/thealgorithms/conversions/DecimalToHexadecimal.java b/src/main/java/com/thealgorithms/conversions/DecimalToHexadecimal.java
new file mode 100644
index 00000000..47a1e36b
--- /dev/null
+++ b/src/main/java/com/thealgorithms/conversions/DecimalToHexadecimal.java
@@ -0,0 +1,42 @@
+package com.thealgorithms.conversions;
+
+/**
+ * This class provides a method to convert a decimal number to a hexadecimal string.
+ */
+final class DecimalToHexadecimal {
+    private static final int SIZE_OF_INT_IN_HALF_BYTES = 8;
+    private static final int NUMBER_OF_BITS_IN_HALF_BYTE = 4;
+    private static final int HALF_BYTE_MASK = 0x0F;
+    private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
+
+    private DecimalToHexadecimal() {
+    }
+
+    /**
+     * Converts a decimal number to a hexadecimal string.
+     * @param decimal the decimal number to convert
+     * @return the hexadecimal representation of the decimal number
+     */
+    public static String decToHex(int decimal) {
+        StringBuilder hexBuilder = new StringBuilder(SIZE_OF_INT_IN_HALF_BYTES);
+        for (int i = SIZE_OF_INT_IN_HALF_BYTES - 1; i >= 0; --i) {
+            int currentHalfByte = decimal & HALF_BYTE_MASK;
+            hexBuilder.insert(0, HEX_DIGITS[currentHalfByte]);
+            decimal >>= NUMBER_OF_BITS_IN_HALF_BYTE;
+        }
+        return removeLeadingZeros(hexBuilder.toString().toLowerCase());
+    }
+
+    private static String removeLeadingZeros(String str) {
+        if (str == null || str.isEmpty()) {
+            return str;
+        }
+
+        int i = 0;
+        while (i < str.length() && str.charAt(i) == '0') {
+            i++;
+        }
+
+        return i == str.length() ? "0" : str.substring(i);
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/conversions/DecimalToHexaDecimalTest.java b/src/test/java/com/thealgorithms/conversions/DecimalToHexaDecimalTest.java
deleted file mode 100644
index 1105f457..00000000
--- a/src/test/java/com/thealgorithms/conversions/DecimalToHexaDecimalTest.java
+++ /dev/null
@@ -1,14 +0,0 @@
-package com.thealgorithms.conversions;
-
-import static org.junit.jupiter.api.Assertions.assertEquals;
-
-import org.junit.jupiter.api.Test;
-
-public class DecimalToHexaDecimalTest {
-
-    @Test
-    public void testDecimalToHexaDecimal() {
-        assertEquals("000000be", DecimalToHexaDecimal.decToHex(190));
-        assertEquals("00000708", DecimalToHexaDecimal.decToHex(1800));
-    }
-}
diff --git a/src/test/java/com/thealgorithms/conversions/DecimalToHexadecimalTest.java b/src/test/java/com/thealgorithms/conversions/DecimalToHexadecimalTest.java
new file mode 100644
index 00000000..c9817b5d
--- /dev/null
+++ b/src/test/java/com/thealgorithms/conversions/DecimalToHexadecimalTest.java
@@ -0,0 +1,14 @@
+package com.thealgorithms.conversions;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+
+public class DecimalToHexadecimalTest {
+    @ParameterizedTest
+    @CsvSource({"0, 0", "1, 1", "10, a", "15, f", "16, 10", "255, ff", "190, be", "1800, 708"})
+    void testDecToHex(int decimal, String expectedHex) {
+        assertEquals(expectedHex, DecimalToHexadecimal.decToHex(decimal));
+    }
+}