Enhance docs, add more tests in EndianConverter (#5921)

This commit is contained in:
Hardik Pawar
2024-10-27 00:05:36 +05:30
committed by GitHub
parent 62c9309a31
commit b5a097c56a
2 changed files with 52 additions and 15 deletions

View File

@@ -1,11 +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
* Utility class for converting integers between big-endian and little-endian formats.
* <p>
* Endianness defines how byte sequences represent multi-byte data types:
* <ul>
* <li><b>Big-endian</b>: The most significant byte (MSB) comes first.</li>
* <li><b>Little-endian</b>: The least significant byte (LSB) comes first.</li>
* </ul>
* <p>
* Example conversion:
* <ul>
* <li>Big-endian to little-endian: {@code 0x12345678} → {@code 0x78563412}</li>
* <li>Little-endian to big-endian: {@code 0x78563412} → {@code 0x12345678}</li>
* </ul>
*
* Little-endian to big-endian: 0x12345678 -> 0x78563412
* <p>Note: Both conversions in this utility are equivalent since reversing the bytes is symmetric.</p>
*
* <p>This class only supports 32-bit integers.</p>
*
* @author Hardvan
*/
@@ -13,10 +25,22 @@ public final class EndianConverter {
private EndianConverter() {
}
/**
* Converts a 32-bit integer from big-endian to little-endian.
*
* @param value the integer in big-endian format
* @return the integer in little-endian format
*/
public static int bigToLittleEndian(int value) {
return Integer.reverseBytes(value);
}
/**
* Converts a 32-bit integer from little-endian to big-endian.
*
* @param value the integer in little-endian format
* @return the integer in big-endian format
*/
public static int littleToBigEndian(int value) {
return Integer.reverseBytes(value);
}