-
Notifications
You must be signed in to change notification settings - Fork 19.6k
/
Copy pathEndianConverter.java
47 lines (44 loc) · 1.43 KB
/
EndianConverter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.thealgorithms.conversions;
/**
* 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>
*
* <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);
}
}