mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-12 14:42:03 +08:00
Add EndianConverter
algorithm (#5751)
This commit is contained in:
@ -89,6 +89,7 @@
|
|||||||
* [DecimalToBinary](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/DecimalToBinary.java)
|
* [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)
|
* [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)
|
* [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)
|
* [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)
|
* [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)
|
* [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)
|
* [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)
|
* [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)
|
* [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)
|
* [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)
|
* [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)
|
* [HexToOctTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/HexToOctTest.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);
|
||||||
|
}
|
||||||
|
}
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user