File tree Expand file tree Collapse file tree 2 files changed +43
-0
lines changed
main/java/com/thealgorithms/conversions
test/java/com/thealgorithms/conversions Expand file tree Collapse file tree 2 files changed +43
-0
lines changed 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 class EndianConverter {
13+
14+ public static int bigToLittleEndian (int value ) {
15+ return Integer .reverseBytes (value );
16+ }
17+
18+ public static int littleToBigEndian (int value ) {
19+ return Integer .reverseBytes (value );
20+ }
21+ }
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