Package com.aurumsmods.littlebigio
Class BitConverter
java.lang.Object
com.aurumsmods.littlebigio.BitConverter
A utility class that provides static methods to convert various primitive
data types into byte representations and vice-versa. Aside from
ByteBuffer
, Java's libraries usually specify and deal with big-endian
data only.-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
getBoolean
(byte[] arr, int off) Returns theboolean
value converted from thebyte
at a specified position in abyte
array.static byte
getByte
(byte[] arr, int off) Returns thebyte
value (-128 to 127) at a specified position in abyte
array.static char
Returns thechar
value (0 to 65535) at a specified position in abyte
array.static double
Returns thedouble
value at a specified position in abyte
array.static float
Returns thefloat
value at a specified position in abyte
array.static int
Returns theint
value (-2147483648 to 2147483647) at a specified position in abyte
array.static long
Returns thelong
value (2^63 to 2^63-1) at a specified position in abyte
array.static short
Returns theshort
value (-32768 to 32767) at a specified position in abyte
array.static int
getUnsignedByte
(byte[] arr, int off) Returns the unsignedbyte
value (0 to 255) at a specified position in abyte
array.static long
getUnsignedInt
(byte[] arr, int off, ByteOrder endian) Returns the unsignedint
value (0 to 4294967295) at a specified position in abyte
array.static int
getUnsignedShort
(byte[] arr, int off, ByteOrder endian) Returns the unsignedshort
value (0 to 65535) at a specified position in abyte
array.static void
putBoolean
(byte[] arr, int off, boolean val) Writes theboolean
value at the specified offset in the raw data array.static void
putByte
(byte[] arr, int off, byte val) Writes thebyte
value at the specified offset in the raw data array.static void
Writes thechar
value at the specified offset in the raw data array.static void
Writes thedouble
value at the specified offset in the raw data array.static void
Writes thefloat
value at the specified offset in the raw data array.static void
Writes theint
value at the specified offset in the raw data array.static void
Writes thelong
value at the specified offset in the raw data array.static void
Writes theshort
value at the specified offset in the raw data array.static byte[]
toBytes
(boolean val) Creates a byte array that fits 1 byte and writes theboolean
value into it.static byte[]
toBytes
(byte val) Creates a byte array that fits 1 byte and writes thebyte
value into it.static byte[]
Creates a byte array that fits 2 bytes and writes thechar
value into it.static byte[]
Creates a byte array that fits 8 bytes and writes thedouble
value into it.static byte[]
Creates a byte array that fits 4 bytes and writes thefloat
value into it.static byte[]
Creates a byte array that fits 4 bytes and writes theint
value into it.static byte[]
Creates a byte array that fits 8 bytes and writes thelong
value into it.static byte[]
Creates a byte array that fits 2 bytes and writes theshort
value into it.
-
Method Details
-
getBoolean
public static boolean getBoolean(byte[] arr, int off) Returns theboolean
value converted from thebyte
at a specified position in abyte
array. This method returnstrue
if thebyte
is nonzero andfalse
if it is zero.- Parameters:
arr
- the byte array.off
- the position in the data array.- Returns:
- the
boolean
value. - Throws:
NullPointerException
- ifarr
isnull
.ArrayIndexOutOfBoundsException
-off < 0 || off >= arr.length
.
-
getByte
public static byte getByte(byte[] arr, int off) Returns thebyte
value (-128 to 127) at a specified position in abyte
array.- Parameters:
arr
- the byte array.off
- the position in the byte array.- Returns:
- the
byte
value. - Throws:
NullPointerException
- ifarr
isnull
.ArrayIndexOutOfBoundsException
-off < 0 || off >= arr.length
.
-
getUnsignedByte
public static int getUnsignedByte(byte[] arr, int off) Returns the unsignedbyte
value (0 to 255) at a specified position in abyte
array.- Parameters:
arr
- the byte array.off
- the position in the byte array.- Returns:
- the
byte
value. - Throws:
NullPointerException
- ifarr
isnull
.ArrayIndexOutOfBoundsException
-off < 0 || off >= arr.length
.
-
getShort
Returns theshort
value (-32768 to 32767) at a specified position in abyte
array. The way the bytes are interpreted depends on the specifiedByteOrder
.- Parameters:
arr
- the byte array.off
- the position in the byte array.endian
- the byte order that specifies how the data will be interpreted.- Returns:
- the
short
value. - Throws:
NullPointerException
- ifarr
orendian
isnull
.ArrayIndexOutOfBoundsException
-off < 0 || off >= arr.length
.
-
getUnsignedShort
Returns the unsignedshort
value (0 to 65535) at a specified position in abyte
array. The way the bytes are interpreted depends on the specifiedByteOrder
.- Parameters:
arr
- the byte array.off
- the position in the byte array.endian
- the byte order that specifies how the data will be interpreted.- Returns:
- the
unsigned short
value. - Throws:
NullPointerException
- ifarr
orendian
isnull
.ArrayIndexOutOfBoundsException
-off < 0 || (off + 1) >= arr.length
.
-
getChar
Returns thechar
value (0 to 65535) at a specified position in abyte
array. The way the bytes are interpreted depends on the specifiedByteOrder
.- Parameters:
arr
- the byte array.off
- the position in the byte array.endian
- the byte order that specifies how the data will be interpreted.- Returns:
- the
char
value. - Throws:
NullPointerException
- ifarr
orendian
isnull
.ArrayIndexOutOfBoundsException
-off < 0 || (off + 1) >= arr.length
.
-
getInt
Returns theint
value (-2147483648 to 2147483647) at a specified position in abyte
array. The way the bytes are interpreted depends on the specifiedByteOrder
.- Parameters:
arr
- the byte array.off
- the position in the byte array.endian
- the byte order that specifies how the data will be interpreted.- Returns:
- the
int
value. - Throws:
NullPointerException
- ifarr
orendian
isnull
.ArrayIndexOutOfBoundsException
-off < 0 || (off + 3) >= arr.length
.
-
getUnsignedInt
Returns the unsignedint
value (0 to 4294967295) at a specified position in abyte
array. The way the bytes are interpreted depends on the specifiedByteOrder
.- Parameters:
arr
- the byte array.off
- the position in the byte array.endian
- the byte order that specifies how the data will be interpreted.- Returns:
- the unsigned
int
value. - Throws:
NullPointerException
- ifarr
orendian
isnull
.ArrayIndexOutOfBoundsException
-off < 0 || (off + 3) >= arr.length
.
-
getLong
Returns thelong
value (2^63 to 2^63-1) at a specified position in abyte
array. The way the bytes are interpreted depends on the specifiedByteOrder
.- Parameters:
arr
- the byte array.off
- the position in the byte array.endian
- the byte order that specifies how the data will be interpreted.- Returns:
- the
long
value. - Throws:
NullPointerException
- ifarr
orendian
isnull
.ArrayIndexOutOfBoundsException
-off < 0 || (off + 7) >= arr.length
.
-
getFloat
Returns thefloat
value at a specified position in abyte
array. The way the bytes are interpreted depends on the specifiedByteOrder
. Essentially, this performs the same action as calling the following:Float.intBitsToFloat(getInt(arr, off, endian))
- Parameters:
arr
- the byte array.off
- the position in the byte array.endian
- the byte order that specifies how the data will be interpreted.- Returns:
- the
float
value. - Throws:
NullPointerException
- ifarr
orendian
isnull
.ArrayIndexOutOfBoundsException
-off < 0 || (off + 3) >= arr.length
.- See Also:
-
getDouble
Returns thedouble
value at a specified position in abyte
array. The way the bytes are interpreted depends on the specifiedByteOrder
. Essentially, this performs the same action as calling the following:Double.longBitsToDouble(getLong(arr, off, endian))
- Parameters:
arr
- the byte array.off
- the position in the byte array.endian
- the byte order that specifies how the data will be interpreted.- Returns:
- the
double
value. - Throws:
NullPointerException
- ifarr
orendian
isnull
.ArrayIndexOutOfBoundsException
-off < 0 || (off + 7) >= arr.length
.- See Also:
-
putBoolean
public static void putBoolean(byte[] arr, int off, boolean val) Writes theboolean
value at the specified offset in the raw data array. If the value istrue
, 1 will be written, otherwise 0.- Parameters:
arr
- the output array.off
- the offset in the data array.val
- theboolean
value.
-
putByte
public static void putByte(byte[] arr, int off, byte val) Writes thebyte
value at the specified offset in the raw data array.- Parameters:
arr
- the output array.off
- the offset in the data array.val
- thebyte
value.
-
putShort
Writes theshort
value at the specified offset in the raw data array. The way the bytes are written depends on the specifiedByteOrder
.- Parameters:
arr
- the output array.off
- the offset in the data array.endian
- the byte order that specifies how the bytes will be written.val
- theshort
value.
-
putChar
Writes thechar
value at the specified offset in the raw data array. The way the bytes are written depends on the specifiedByteOrder
.- Parameters:
arr
- the output array.off
- the offset in the data array.endian
- the byte order that specifies how the bytes will be written.val
- thechar
value.- See Also:
-
putInt
Writes theint
value at the specified offset in the raw data array. The way the bytes are written depends on the specifiedByteOrder
.- Parameters:
arr
- the output array.off
- the offset in the data array.endian
- the byte order that specifies how the bytes will be written.val
- theint
value.
-
putLong
Writes thelong
value at the specified offset in the raw data array. The way the bytes are written depends on the specifiedByteOrder
.- Parameters:
arr
- the output array.off
- the offset in the data array.endian
- the byte order that specifies how the bytes will be written.val
- thelong
value.
-
putFloat
Writes thefloat
value at the specified offset in the raw data array. The way the bytes are written depends on the specifiedByteOrder
.- Parameters:
arr
- the output array.off
- the offset in the data array.endian
- the byte order that specifies how the bytes will be written.val
- thefloat
value.- See Also:
-
putDouble
Writes thedouble
value at the specified offset in the raw data array. The way the bytes are written depends on the specifiedByteOrder
.- Parameters:
arr
- the output array.off
- the offset in the data array.endian
- the byte order that specifies how the bytes will be written.val
- thedouble
value.- See Also:
-
toBytes
public static byte[] toBytes(boolean val) Creates a byte array that fits 1 byte and writes theboolean
value into it. If the value istrue
, 1 is written, otherwise 0 is written.- Parameters:
val
- theboolean
value.- Returns:
- the array containing the
boolean
value.
-
toBytes
public static byte[] toBytes(byte val) Creates a byte array that fits 1 byte and writes thebyte
value into it.- Parameters:
val
- thebyte
value.- Returns:
- the array containing the
byte
value.
-
toBytes
Creates a byte array that fits 2 bytes and writes theshort
value into it.- Parameters:
val
- theshort
value.endian
- the byte order in which the bytes are written.- Returns:
- the array containing the
short
value.
-
toBytes
Creates a byte array that fits 2 bytes and writes thechar
value into it.- Parameters:
val
- thechar
value.endian
- the byte order in which the bytes are written.- Returns:
- the array containing the
char
value.
-
toBytes
Creates a byte array that fits 4 bytes and writes theint
value into it.- Parameters:
val
- theint
value.endian
- the byte order in which the bytes are written.- Returns:
- the array containing the
int
value.
-
toBytes
Creates a byte array that fits 8 bytes and writes thelong
value into it.- Parameters:
val
- thelong
value.endian
- the byte order in which the bytes are written.- Returns:
- the array containing the
long
value.
-
toBytes
Creates a byte array that fits 4 bytes and writes thefloat
value into it.- Parameters:
val
- thefloat
value.endian
- the byte order in which the bytes are written.- Returns:
- the array containing the
float
value.
-
toBytes
Creates a byte array that fits 8 bytes and writes thedouble
value into it.- Parameters:
val
- thedouble
value.endian
- the byte order in which the bytes are written.- Returns:
- the array containing the
double
value.
-