Skip to content

Commit 0b5011e

Browse files
committed
feat: Add EndianConverter new algorithm with Junit tests
1 parent 4a03f42 commit 0b5011e

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}

0 commit comments

Comments
 (0)