public final class EndianBitConverter extends Object
BitConverter
assumes that the byte arrays given to it are always in
the system's native byte order. If you want to convert bytes that come from a file
that is always in the same byte order regardless of the system's endianness, this class will
do the byte reordering for you.Modifier and Type | Method and Description |
---|---|
void |
fromDouble(double value,
byte[] bytes,
int startIndex)
Converts the specified double-precision floating point value and stores the bytes in the given array.
|
void |
fromInt16(short value,
byte[] bytes,
int startIndex)
Converts the specified 16-bit signed integer value and stores the bytes in the given array.
|
void |
fromInt32(int value,
byte[] bytes,
int startIndex)
Converts the specified 32-bit signed integer value and stores the bytes in the given array.
|
void |
fromInt64(long value,
byte[] bytes,
int startIndex)
Converts the specified 64-bit signed integer value and stores the bytes in the given array.
|
void |
fromUInt16(int value,
byte[] bytes,
int startIndex)
Converts the specified 16-bit unsigned integer value and stores the bytes in the given array.
|
void |
fromUInt32(long value,
byte[] bytes,
int startIndex)
Converts the specified 32-bit unsigned integer value and stores the bytes in the given array.
|
static EndianBitConverter |
getBigEndian()
Gets an
EndianBitConverter that assumes all bytes are in Big Endian byte order. |
byte[] |
getBytes(double value)
Returns the specified double-precision floating point value as an array of bytes.
|
byte[] |
getBytes(float value)
Returns the specified single-precision floating point value as an array of bytes.
|
byte[] |
getBytes(int value)
Returns the specified 32-bit signed integer value as an array of bytes.
|
byte[] |
getBytes(long value)
Returns the specified 64-bit signed integer value as an array of bytes.
|
byte[] |
getBytes(short value)
Returns the specified 16-bit signed integer value as an array of bytes.
|
static EndianBitConverter |
getLittleEndian()
Gets an
EndianBitConverter that assumes all bytes are in Little Endian byte order. |
double |
toDouble(byte[] value,
int startIndex)
Returns a double-precision floating point number converted from eight bytes at a specified position in a byte array.
|
short |
toInt16(byte[] value,
int startIndex)
Returns a 16-bit signed integer converted from two bytes at a specified position in a byte array.
|
int |
toInt32(byte[] value,
int startIndex)
Returns a 32-bit signed integer converted from four bytes at a specified position in a byte array.
|
long |
toInt64(byte[] value,
int startIndex)
Returns a 64-bit signed integer converted from eight bytes at a specified position in a byte array.
|
float |
toSingle(byte[] value,
int startIndex)
Returns a single-precision floating point number converted from four bytes at a specified position in a byte array.
|
int |
toUInt16(byte[] value,
int startIndex)
Returns a 16-bit unsigned integer converted from two bytes at a specified position in a byte array.
|
long |
toUInt32(byte[] value,
int startIndex)
Returns a 32-bit unsigned integer converted from four bytes at a specified position in a byte array.
|
@Nonnull public static EndianBitConverter getLittleEndian()
EndianBitConverter
that assumes all bytes are in Little Endian byte order.@Nonnull public static EndianBitConverter getBigEndian()
EndianBitConverter
that assumes all bytes are in Big Endian byte order.public final short toInt16(@Nonnull byte[] value, int startIndex)
value
- An array of bytes.startIndex
- The starting position within value.startIndex
.@Nonnull public final byte[] getBytes(short value)
value
- The number to convert.public final void fromInt16(short value, @Nonnull byte[] bytes, int startIndex)
value
- The number to convert.bytes
- An array of bytes.startIndex
- The starting position within bytes.public final int toUInt16(@Nonnull byte[] value, int startIndex)
value
- An array of bytes.startIndex
- The starting position within value.startIndex
.public final void fromUInt16(int value, @Nonnull byte[] bytes, int startIndex)
value
- The number to convert.bytes
- An array of bytes.startIndex
- The starting position within bytes.public final int toInt32(@Nonnull byte[] value, int startIndex)
value
- An array of bytes.startIndex
- The starting position within value.startIndex
.@Nonnull public final byte[] getBytes(int value)
value
- The number to convert.public final void fromInt32(int value, @Nonnull byte[] bytes, int startIndex)
value
- The number to convert.bytes
- An array of bytes.startIndex
- The starting position within bytes.public final long toUInt32(@Nonnull byte[] value, int startIndex)
value
- An array of bytes.startIndex
- The starting position within value.startIndex
.public final void fromUInt32(long value, @Nonnull byte[] bytes, int startIndex)
value
- The number to convert.bytes
- An array of bytes.startIndex
- The starting position within bytes.public final float toSingle(@Nonnull byte[] value, int startIndex)
value
- An array of bytes.startIndex
- The starting position within value.startIndex
.@Nonnull public final byte[] getBytes(float value)
value
- The number to convert.public final double toDouble(@Nonnull byte[] value, int startIndex)
value
- An array of bytes.startIndex
- The starting position within value.startIndex
.@Nonnull public final byte[] getBytes(double value)
value
- The number to convert.public final void fromDouble(double value, @Nonnull byte[] bytes, int startIndex)
value
- The number to convert.bytes
- An array of bytes.startIndex
- The starting position within bytes.public final long toInt64(@Nonnull byte[] value, int startIndex)
value
- An array of bytes.startIndex
- The starting position within value.startIndex
.@Nonnull public final byte[] getBytes(long value)
value
- The number to convert.public final void fromInt64(long value, @Nonnull byte[] bytes, int startIndex)
value
- The number to convert.bytes
- An array of bytes.startIndex
- The starting position within bytes.