Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,22 +1,46 @@
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
*/
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,34 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

public class EndianConverterTest {

@Test
public void testBigToLittleEndian() {
assertEquals(0x78563412, EndianConverter.bigToLittleEndian(0x12345678));
assertEquals(0x00000000, EndianConverter.bigToLittleEndian(0x00000000));
assertEquals(0x00000001, EndianConverter.bigToLittleEndian(0x01000000));
@ParameterizedTest
@CsvSource({
"0x78563412, 0x12345678", "0x00000000, 0x00000000", "0x00000001, 0x01000000",
"0xFFFFFFFF, 0xFFFFFFFF", // -1 in two's complement
"0x0000007F, 0x7F000000" // Positive boundary case
})
public void
testLittleToBigEndian(String inputHex, String expectedHex) {
int input = (int) Long.parseLong(inputHex.substring(2), 16); // Convert hex string to int
int expected = (int) Long.parseLong(expectedHex.substring(2), 16); // Convert hex string to int
assertEquals(expected, EndianConverter.littleToBigEndian(input));
}

@Test
public void testLittleToBigEndian() {
assertEquals(0x12345678, EndianConverter.littleToBigEndian(0x78563412));
assertEquals(0x00000000, EndianConverter.littleToBigEndian(0x00000000));
assertEquals(0x01000000, EndianConverter.littleToBigEndian(0x00000001));
@ParameterizedTest
@CsvSource({
"0x12345678, 0x78563412", "0x00000000, 0x00000000", "0x01000000, 0x00000001",
"0xFFFFFFFF, 0xFFFFFFFF", // -1 in two's complement
"0x7F000000, 0x0000007F" // Positive boundary case
})
public void
testBigToLittleEndian(String inputHex, String expectedHex) {
int input = (int) Long.parseLong(inputHex.substring(2), 16); // Convert hex string to int
int expected = (int) Long.parseLong(expectedHex.substring(2), 16); // Convert hex string to int
assertEquals(expected, EndianConverter.bigToLittleEndian(input));
}
}