Class BitConverter

java.lang.Object
com.aurumsmods.littlebigio.BitConverter

public final class BitConverter extends Object
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 Type
    Method
    Description
    static boolean
    getBoolean(byte[] arr, int off)
    Returns the boolean value converted from the byte at a specified position in a byte array.
    static byte
    getByte(byte[] arr, int off)
    Returns the byte value (-128 to 127) at a specified position in a byte array.
    static char
    getChar(byte[] arr, int off, ByteOrder endian)
    Returns the char value (0 to 65535) at a specified position in a byte array.
    static double
    getDouble(byte[] arr, int off, ByteOrder endian)
    Returns the double value at a specified position in a byte array.
    static float
    getFloat(byte[] arr, int off, ByteOrder endian)
    Returns the float value at a specified position in a byte array.
    static int
    getInt(byte[] arr, int off, ByteOrder endian)
    Returns the int value (-2147483648 to 2147483647) at a specified position in a byte array.
    static long
    getLong(byte[] arr, int off, ByteOrder endian)
    Returns the long value (2^63 to 2^63-1) at a specified position in a byte array.
    static short
    getShort(byte[] arr, int off, ByteOrder endian)
    Returns the short value (-32768 to 32767) at a specified position in a byte array.
    static int
    getUnsignedByte(byte[] arr, int off)
    Returns the unsigned byte value (0 to 255) at a specified position in a byte array.
    static long
    getUnsignedInt(byte[] arr, int off, ByteOrder endian)
    Returns the unsigned int value (0 to 4294967295) at a specified position in a byte array.
    static int
    getUnsignedShort(byte[] arr, int off, ByteOrder endian)
    Returns the unsigned short value (0 to 65535) at a specified position in a byte array.
    static void
    putBoolean(byte[] arr, int off, boolean val)
    Writes the boolean value at the specified offset in the raw data array.
    static void
    putByte(byte[] arr, int off, byte val)
    Writes the byte value at the specified offset in the raw data array.
    static void
    putChar(byte[] arr, int off, ByteOrder endian, char val)
    Writes the char value at the specified offset in the raw data array.
    static void
    putDouble(byte[] arr, int off, ByteOrder endian, double val)
    Writes the double value at the specified offset in the raw data array.
    static void
    putFloat(byte[] arr, int off, ByteOrder endian, float val)
    Writes the float value at the specified offset in the raw data array.
    static void
    putInt(byte[] arr, int off, ByteOrder endian, int val)
    Writes the int value at the specified offset in the raw data array.
    static void
    putLong(byte[] arr, int off, ByteOrder endian, long val)
    Writes the long value at the specified offset in the raw data array.
    static void
    putShort(byte[] arr, int off, ByteOrder endian, short val)
    Writes the short 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 the boolean value into it.
    static byte[]
    toBytes(byte val)
    Creates a byte array that fits 1 byte and writes the byte value into it.
    static byte[]
    toBytes(char val, ByteOrder endian)
    Creates a byte array that fits 2 bytes and writes the char value into it.
    static byte[]
    toBytes(double val, ByteOrder endian)
    Creates a byte array that fits 8 bytes and writes the double value into it.
    static byte[]
    toBytes(float val, ByteOrder endian)
    Creates a byte array that fits 4 bytes and writes the float value into it.
    static byte[]
    toBytes(int val, ByteOrder endian)
    Creates a byte array that fits 4 bytes and writes the int value into it.
    static byte[]
    toBytes(long val, ByteOrder endian)
    Creates a byte array that fits 8 bytes and writes the long value into it.
    static byte[]
    toBytes(short val, ByteOrder endian)
    Creates a byte array that fits 2 bytes and writes the short value into it.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getBoolean

      public static boolean getBoolean(byte[] arr, int off)
      Returns the boolean value converted from the byte at a specified position in a byte array. This method returns true if the byte is nonzero and false if it is zero.
      Parameters:
      arr - the byte array.
      off - the position in the data array.
      Returns:
      the boolean value.
      Throws:
      NullPointerException - if arr is null.
      ArrayIndexOutOfBoundsException - off < 0 || off >= arr.length.
    • getByte

      public static byte getByte(byte[] arr, int off)
      Returns the byte value (-128 to 127) at a specified position in a byte array.
      Parameters:
      arr - the byte array.
      off - the position in the byte array.
      Returns:
      the byte value.
      Throws:
      NullPointerException - if arr is null.
      ArrayIndexOutOfBoundsException - off < 0 || off >= arr.length.
    • getUnsignedByte

      public static int getUnsignedByte(byte[] arr, int off)
      Returns the unsigned byte value (0 to 255) at a specified position in a byte array.
      Parameters:
      arr - the byte array.
      off - the position in the byte array.
      Returns:
      the byte value.
      Throws:
      NullPointerException - if arr is null.
      ArrayIndexOutOfBoundsException - off < 0 || off >= arr.length.
    • getShort

      public static short getShort(byte[] arr, int off, ByteOrder endian)
      Returns the short value (-32768 to 32767) at a specified position in a byte array. The way the bytes are interpreted depends on the specified ByteOrder.
      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 - if arr or endian is null.
      ArrayIndexOutOfBoundsException - off < 0 || off >= arr.length.
    • getUnsignedShort

      public static int getUnsignedShort(byte[] arr, int off, ByteOrder endian)
      Returns the unsigned short value (0 to 65535) at a specified position in a byte array. The way the bytes are interpreted depends on the specified ByteOrder.
      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 - if arr or endian is null.
      ArrayIndexOutOfBoundsException - off < 0 || (off + 1) >= arr.length.
    • getChar

      public static char getChar(byte[] arr, int off, ByteOrder endian)
      Returns the char value (0 to 65535) at a specified position in a byte array. The way the bytes are interpreted depends on the specified ByteOrder.
      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 - if arr or endian is null.
      ArrayIndexOutOfBoundsException - off < 0 || (off + 1) >= arr.length.
    • getInt

      public static int getInt(byte[] arr, int off, ByteOrder endian)
      Returns the int value (-2147483648 to 2147483647) at a specified position in a byte array. The way the bytes are interpreted depends on the specified ByteOrder.
      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 - if arr or endian is null.
      ArrayIndexOutOfBoundsException - off < 0 || (off + 3) >= arr.length.
    • getUnsignedInt

      public static long getUnsignedInt(byte[] arr, int off, ByteOrder endian)
      Returns the unsigned int value (0 to 4294967295) at a specified position in a byte array. The way the bytes are interpreted depends on the specified ByteOrder.
      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 - if arr or endian is null.
      ArrayIndexOutOfBoundsException - off < 0 || (off + 3) >= arr.length.
    • getLong

      public static long getLong(byte[] arr, int off, ByteOrder endian)
      Returns the long value (2^63 to 2^63-1) at a specified position in a byte array. The way the bytes are interpreted depends on the specified ByteOrder.
      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 - if arr or endian is null.
      ArrayIndexOutOfBoundsException - off < 0 || (off + 7) >= arr.length.
    • getFloat

      public static float getFloat(byte[] arr, int off, ByteOrder endian)
      Returns the float value at a specified position in a byte array. The way the bytes are interpreted depends on the specified ByteOrder. 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 - if arr or endian is null.
      ArrayIndexOutOfBoundsException - off < 0 || (off + 3) >= arr.length.
      See Also:
    • getDouble

      public static double getDouble(byte[] arr, int off, ByteOrder endian)
      Returns the double value at a specified position in a byte array. The way the bytes are interpreted depends on the specified ByteOrder. 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 - if arr or endian is null.
      ArrayIndexOutOfBoundsException - off < 0 || (off + 7) >= arr.length.
      See Also:
    • putBoolean

      public static void putBoolean(byte[] arr, int off, boolean val)
      Writes the boolean value at the specified offset in the raw data array. If the value is true, 1 will be written, otherwise 0.
      Parameters:
      arr - the output array.
      off - the offset in the data array.
      val - the boolean value.
    • putByte

      public static void putByte(byte[] arr, int off, byte val)
      Writes the byte value at the specified offset in the raw data array.
      Parameters:
      arr - the output array.
      off - the offset in the data array.
      val - the byte value.
    • putShort

      public static void putShort(byte[] arr, int off, ByteOrder endian, short val)
      Writes the short value at the specified offset in the raw data array. The way the bytes are written depends on the specified ByteOrder.
      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 - the short value.
    • putChar

      public static void putChar(byte[] arr, int off, ByteOrder endian, char val)
      Writes the char value at the specified offset in the raw data array. The way the bytes are written depends on the specified ByteOrder.
      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 - the char value.
      See Also:
    • putInt

      public static void putInt(byte[] arr, int off, ByteOrder endian, int val)
      Writes the int value at the specified offset in the raw data array. The way the bytes are written depends on the specified ByteOrder.
      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 - the int value.
    • putLong

      public static void putLong(byte[] arr, int off, ByteOrder endian, long val)
      Writes the long value at the specified offset in the raw data array. The way the bytes are written depends on the specified ByteOrder.
      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 - the long value.
    • putFloat

      public static void putFloat(byte[] arr, int off, ByteOrder endian, float val)
      Writes the float value at the specified offset in the raw data array. The way the bytes are written depends on the specified ByteOrder.
      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 - the float value.
      See Also:
    • putDouble

      public static void putDouble(byte[] arr, int off, ByteOrder endian, double val)
      Writes the double value at the specified offset in the raw data array. The way the bytes are written depends on the specified ByteOrder.
      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 - the double value.
      See Also:
    • toBytes

      public static byte[] toBytes(boolean val)
      Creates a byte array that fits 1 byte and writes the boolean value into it. If the value is true, 1 is written, otherwise 0 is written.
      Parameters:
      val - the boolean 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 the byte value into it.
      Parameters:
      val - the byte value.
      Returns:
      the array containing the byte value.
    • toBytes

      public static byte[] toBytes(short val, ByteOrder endian)
      Creates a byte array that fits 2 bytes and writes the short value into it.
      Parameters:
      val - the short value.
      endian - the byte order in which the bytes are written.
      Returns:
      the array containing the short value.
    • toBytes

      public static byte[] toBytes(char val, ByteOrder endian)
      Creates a byte array that fits 2 bytes and writes the char value into it.
      Parameters:
      val - the char value.
      endian - the byte order in which the bytes are written.
      Returns:
      the array containing the char value.
    • toBytes

      public static byte[] toBytes(int val, ByteOrder endian)
      Creates a byte array that fits 4 bytes and writes the int value into it.
      Parameters:
      val - the int value.
      endian - the byte order in which the bytes are written.
      Returns:
      the array containing the int value.
    • toBytes

      public static byte[] toBytes(long val, ByteOrder endian)
      Creates a byte array that fits 8 bytes and writes the long value into it.
      Parameters:
      val - the long value.
      endian - the byte order in which the bytes are written.
      Returns:
      the array containing the long value.
    • toBytes

      public static byte[] toBytes(float val, ByteOrder endian)
      Creates a byte array that fits 4 bytes and writes the float value into it.
      Parameters:
      val - the float value.
      endian - the byte order in which the bytes are written.
      Returns:
      the array containing the float value.
    • toBytes

      public static byte[] toBytes(double val, ByteOrder endian)
      Creates a byte array that fits 8 bytes and writes the double value into it.
      Parameters:
      val - the double value.
      endian - the byte order in which the bytes are written.
      Returns:
      the array containing the double value.