Repository: TheAlgorithms/Java
Pull request #5751 (2024-10-13 03:51:20)

feat: Add `EndianConverter` new algorithm with Junit tests
<!--
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/5751?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
Attention: Patch coverage is `66.66667%` with `1 line` in your changes missing coverage. Please review.
> Project coverage is 63.92%. Comparing base [(`4a03f42`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/4a03f420616a6773e9a5cdc304b1681e96d945bd?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`7676925`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/7676925344b2f8f5f5423f6a245174b80c8b9129?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

| [Files with missing lines](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5751?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...com/thealgorithms/conversions/EndianConverter.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5751?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fconversions%2FEndianConverter.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9jb252ZXJzaW9ucy9FbmRpYW5Db252ZXJ0ZXIuamF2YQ==) | 66.66% | [1 Missing :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5751?src=pr&el=tree&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    #5751      +/-   ##
============================================
+ Coverage     63.91%   63.92%   +0.01%     
- Complexity     4167     4170       +3     
============================================
  Files           582      583       +1     
  Lines         16302    16305       +3     
  Branches       3148     3148              
============================================
+ Hits          10419    10423       +4     
  Misses         5457     5457              
+ Partials        426      425       -1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5751?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/DIRECTORY.md b/DIRECTORY.md
index 342aa37a..eeb50764 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -89,6 +89,7 @@
             * [DecimalToBinary](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/DecimalToBinary.java)
             * [DecimalToHexadecimal](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/DecimalToHexadecimal.java)
             * [DecimalToOctal](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/DecimalToOctal.java)
+            * [EndianConverter](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/EndianConverter.java)
             * [HexaDecimalToBinary](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/HexaDecimalToBinary.java)
             * [HexaDecimalToDecimal](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/HexaDecimalToDecimal.java)
             * [HexToOct](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/HexToOct.java)
@@ -728,6 +729,7 @@
             * [DecimalToBinaryTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/DecimalToBinaryTest.java)
             * [DecimalToHexadecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/DecimalToHexadecimalTest.java)
             * [DecimalToOctalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/DecimalToOctalTest.java)
+            * [EndianConverterTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/EndianConverterTest.java)
             * [HexaDecimalToBinaryTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/HexaDecimalToBinaryTest.java)
             * [HexaDecimalToDecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/HexaDecimalToDecimalTest.java)
             * [HexToOctTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/HexToOctTest.java)
diff --git a/src/main/java/com/thealgorithms/conversions/EndianConverter.java b/src/main/java/com/thealgorithms/conversions/EndianConverter.java
new file mode 100644
index 00000000..d20d9d6d
--- /dev/null
+++ b/src/main/java/com/thealgorithms/conversions/EndianConverter.java
@@ -0,0 +1,23 @@
+package com.thealgorithms.conversions;
+
+/**
+ * Converts between big-endian and little-endian formats.
+ * Big-endian is the most significant byte first, while little-endian is the least significant byte first.
+ * Big-endian to little-endian: 0x12345678 -> 0x78563412
+ *
+ * Little-endian to big-endian: 0x12345678 -> 0x78563412
+ *
+ * @author Hardvan
+ */
+public final class EndianConverter {
+    private EndianConverter() {
+    }
+
+    public static int bigToLittleEndian(int value) {
+        return Integer.reverseBytes(value);
+    }
+
+    public static int littleToBigEndian(int value) {
+        return Integer.reverseBytes(value);
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/conversions/EndianConverterTest.java b/src/test/java/com/thealgorithms/conversions/EndianConverterTest.java
new file mode 100644
index 00000000..9598dd16
--- /dev/null
+++ b/src/test/java/com/thealgorithms/conversions/EndianConverterTest.java
@@ -0,0 +1,22 @@
+package com.thealgorithms.conversions;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.api.Test;
+
+public class EndianConverterTest {
+
+    @Test
+    public void testBigToLittleEndian() {
+        assertEquals(0x78563412, EndianConverter.bigToLittleEndian(0x12345678));
+        assertEquals(0x00000000, EndianConverter.bigToLittleEndian(0x00000000));
+        assertEquals(0x00000001, EndianConverter.bigToLittleEndian(0x01000000));
+    }
+
+    @Test
+    public void testLittleToBigEndian() {
+        assertEquals(0x12345678, EndianConverter.littleToBigEndian(0x78563412));
+        assertEquals(0x00000000, EndianConverter.littleToBigEndian(0x00000000));
+        assertEquals(0x01000000, EndianConverter.littleToBigEndian(0x00000001));
+    }
+}