File tree Expand file tree Collapse file tree 3 files changed +47
-0
lines changed
main/java/com/thealgorithms/conversions
test/java/com/thealgorithms/conversions Expand file tree Collapse file tree 3 files changed +47
-0
lines changed Original file line number Diff line number Diff line change 8989 * [ DecimalToBinary] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/DecimalToBinary.java )
9090 * [ DecimalToHexadecimal] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/DecimalToHexadecimal.java )
9191 * [ DecimalToOctal] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/DecimalToOctal.java )
92+ * [ EndianConverter] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/EndianConverter.java )
9293 * [ HexaDecimalToBinary] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/HexaDecimalToBinary.java )
9394 * [ HexaDecimalToDecimal] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/HexaDecimalToDecimal.java )
9495 * [ HexToOct] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/HexToOct.java )
728729 * [ DecimalToBinaryTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/DecimalToBinaryTest.java )
729730 * [ DecimalToHexadecimalTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/DecimalToHexadecimalTest.java )
730731 * [ DecimalToOctalTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/DecimalToOctalTest.java )
732+ * [ EndianConverterTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/EndianConverterTest.java )
731733 * [ HexaDecimalToBinaryTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/HexaDecimalToBinaryTest.java )
732734 * [ HexaDecimalToDecimalTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/HexaDecimalToDecimalTest.java )
733735 * [ HexToOctTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/HexToOctTest.java )
Original file line number Diff line number Diff line change 1+ package com .thealgorithms .conversions ;
2+
3+ /**
4+ * Converts between big-endian and little-endian formats.
5+ * Big-endian is the most significant byte first, while little-endian is the least significant byte first.
6+ * Big-endian to little-endian: 0x12345678 -> 0x78563412
7+ *
8+ * Little-endian to big-endian: 0x12345678 -> 0x78563412
9+ *
10+ * @author Hardvan
11+ */
12+ public final class EndianConverter {
13+ private EndianConverter () {
14+ }
15+
16+ public static int bigToLittleEndian (int value ) {
17+ return Integer .reverseBytes (value );
18+ }
19+
20+ public static int littleToBigEndian (int value ) {
21+ return Integer .reverseBytes (value );
22+ }
23+ }
Original file line number Diff line number Diff line change 1+ package com .thealgorithms .conversions ;
2+
3+ import static org .junit .jupiter .api .Assertions .assertEquals ;
4+
5+ import org .junit .jupiter .api .Test ;
6+
7+ public class EndianConverterTest {
8+
9+ @ Test
10+ public void testBigToLittleEndian () {
11+ assertEquals (0x78563412 , EndianConverter .bigToLittleEndian (0x12345678 ));
12+ assertEquals (0x00000000 , EndianConverter .bigToLittleEndian (0x00000000 ));
13+ assertEquals (0x00000001 , EndianConverter .bigToLittleEndian (0x01000000 ));
14+ }
15+
16+ @ Test
17+ public void testLittleToBigEndian () {
18+ assertEquals (0x12345678 , EndianConverter .littleToBigEndian (0x78563412 ));
19+ assertEquals (0x00000000 , EndianConverter .littleToBigEndian (0x00000000 ));
20+ assertEquals (0x01000000 , EndianConverter .littleToBigEndian (0x00000001 ));
21+ }
22+ }
You can’t perform that action at this time.
0 commit comments