Class EndianUtil

java.lang.Object
com.pnfsoftware.jeb.util.io.EndianUtil

public class EndianUtil extends Object
Byte array to/from primitives with endianness support.
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    bigEndianBytesToInt(byte[] array)
    Decode a big-endian 32-bit integer at offset 0.
    static int
    bigEndianBytesToInt(byte[] array, int offset)
    Decode a big-endian 32-bit integer.
    static long
    bigEndianBytesToLong(byte[] array)
    Decode a big-endian 64-bit integer at offset 0.
    static long
    bigEndianBytesToLong(byte[] array, int offset)
    Decode a big-endian 64-bit integer.
    static short
    bigEndianBytesToShort(byte[] array)
    Decode a big-endian 16-bit integer at offset 0.
    static short
    bigEndianBytesToShort(byte[] array, int offset)
    Decode a big-endian 16-bit integer.
    static int
    bytesToInt(byte[] array, int offset, ByteOrder order)
    Read 4 bytes and convert it to an integer.
    static long
    bytesToLong(byte[] array, int offset, ByteOrder order)
    Read 8 bytes and convert it to a long.
    static long
    Convert a byte array to a number, sign is extended to long, meaning that {0x88} will result in 0xFFFFFFFF_FFFFFF88L
    static long
    Convert a byte array to a number, zero extended to long, meaning that {0x88} will result in 0x88L
    static short
    bytesToShort(byte[] array, int offset, ByteOrder order)
    Read 2 bytes and convert it to a short.
    static byte[]
    intToBEBytes(int v)
    Convert a 32-bit integer to a big-endian encoded array of bytes.
    static void
    intToBEBytes(int v, byte[] output)
    Encode a 32-bit integer to big-endian bytes.
    static void
    intToBEBytes(int v, byte[] output, int offset)
    Encode a 32-bit integer to big-endian bytes.
    static byte[]
    intToLEBytes(int v)
    Convert a 32-bit integer to a little-endian encoded array of bytes.
    static void
    intToLEBytes(int v, byte[] output)
    Encode a 32-bit integer to little-endian bytes.
    static void
    intToLEBytes(int v, byte[] output, int offset)
    Encode a 32-bit integer to little-endian bytes.
    static int
    littleEndianBytesToInt(byte[] array)
    Decode a little-endian 32-bit integer at offset 0.
    static int
    littleEndianBytesToInt(byte[] array, int offset)
    Decode a little-endian 32-bit integer.
    static long
    Decode a little-endian 64-bit integer at offset 0.
    static long
    littleEndianBytesToLong(byte[] array, int offset)
    Decode a little-endian 64-bit integer.
    static short
    Decode a little-endian 16-bit integer at offset 0.
    static short
    littleEndianBytesToShort(byte[] array, int offset)
    Decode a little-endian 16-bit integer.
    static byte[]
    longToBEBytes(long v)
    Convert a 64-bit integer to a big-endian encoded array of bytes.
    static void
    longToBEBytes(long v, byte[] output)
    Encode a 64-bit integer to big-endian bytes.
    static void
    longToBEBytes(long v, byte[] output, int offset)
    Encode a 64-bit integer to big-endian bytes.
    static byte[]
    longToLEBytes(long v)
    Convert a 64-bit integer to a little-endian encoded array of bytes.
    static void
    longToLEBytes(long v, byte[] output)
    Encode a 64-bit integer to little-endian bytes.
    static void
    longToLEBytes(long v, byte[] output, int offset)
    Encode a 64-bit integer to little-endian bytes.
    static void
    numberToBytes(ByteOrder bo, long v, byte[] output)
    Automatically convert a value depending on the byte array size.
    static byte[]
    shortToBEBytes(short v)
    Convert a 16-bit integer to a big-endian encoded array of bytes.
    static void
    shortToBEBytes(short v, byte[] output)
    Encode a 16-bit integer to big-endian bytes.
    static void
    shortToBEBytes(short v, byte[] output, int offset)
    Encode a 16-bit integer to big-endian bytes.
    static byte[]
    shortToLEBytes(short v)
    Convert a 16-bit integer to a little-endian encoded array of bytes.
    static void
    shortToLEBytes(short v, byte[] output)
    Encode a 16-bit integer to little-endian bytes.
    static void
    shortToLEBytes(short v, byte[] output, int offset)
    Encode a 16-bit integer to little-endian bytes.
    static void
    swap(byte[] array)
    Flip the byte order of an array of bytes.
    static void
    swap(byte[] array, int offset, int size)
    Flip the byte order of an array of bytes.
    static void
    swapByGroup(byte[] array, int grpByteCount)
    Flip the byte order of groups of bytes.
    static int
    swapInt(int v)
    Swap the byte ordering of an int.
    static long
    swapLong(long v)
    Swap the byte ordering of a long.
    static short
    swapShort(short v)
    Swap the byte ordering of a short.

    Methods inherited from class java.lang.Object

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

    • swap

      public static void swap(byte[] array, int offset, int size)
      Flip the byte order of an array of bytes. This method does not create a new array.
      Parameters:
      array - the input/output array
      offset - start index
      size - number of elements to flip
    • swap

      public static void swap(byte[] array)
      Flip the byte order of an array of bytes. This method does not create a new array.
      Parameters:
      array - the input/output array
    • swapByGroup

      public static void swapByGroup(byte[] array, int grpByteCount)
      Flip the byte order of groups of bytes. This method does not create a new array.
      Parameters:
      array - byte array to change
      grpByteCount - number of bytes per group (2 for 16 bits, 4 for 32 bits, 8 for 64 bits)
    • swapShort

      public static short swapShort(short v)
      Swap the byte ordering of a short.
      Parameters:
      v - input value
      Returns:
      byte-swapped value
    • swapInt

      public static int swapInt(int v)
      Swap the byte ordering of an int.
      Parameters:
      v - input value
      Returns:
      byte-swapped value
    • swapLong

      public static long swapLong(long v)
      Swap the byte ordering of a long.
      Parameters:
      v - input value
      Returns:
      byte-swapped value
    • shortToLEBytes

      public static byte[] shortToLEBytes(short v)
      Convert a 16-bit integer to a little-endian encoded array of bytes.
      Parameters:
      v - input value
      Returns:
      two-byte little-endian representation
    • shortToLEBytes

      public static void shortToLEBytes(short v, byte[] output)
      Encode a 16-bit integer to little-endian bytes.
      Parameters:
      v - input value
      output - output buffer
    • shortToLEBytes

      public static void shortToLEBytes(short v, byte[] output, int offset)
      Encode a 16-bit integer to little-endian bytes.
      Parameters:
      v - input value
      output - output buffer
      offset - output offset
    • intToLEBytes

      public static byte[] intToLEBytes(int v)
      Convert a 32-bit integer to a little-endian encoded array of bytes.
      Parameters:
      v - input value
      Returns:
      four-byte little-endian representation
    • intToLEBytes

      public static void intToLEBytes(int v, byte[] output)
      Encode a 32-bit integer to little-endian bytes.
      Parameters:
      v - input value
      output - output buffer
    • intToLEBytes

      public static void intToLEBytes(int v, byte[] output, int offset)
      Encode a 32-bit integer to little-endian bytes.
      Parameters:
      v - input value
      output - output buffer
      offset - output offset
    • longToLEBytes

      public static byte[] longToLEBytes(long v)
      Convert a 64-bit integer to a little-endian encoded array of bytes.
      Parameters:
      v - input value
      Returns:
      eight-byte little-endian representation
    • longToLEBytes

      public static void longToLEBytes(long v, byte[] output)
      Encode a 64-bit integer to little-endian bytes.
      Parameters:
      v - input value
      output - output buffer
    • longToLEBytes

      public static void longToLEBytes(long v, byte[] output, int offset)
      Encode a 64-bit integer to little-endian bytes.
      Parameters:
      v - input value
      output - output buffer
      offset - output offset
    • shortToBEBytes

      public static byte[] shortToBEBytes(short v)
      Convert a 16-bit integer to a big-endian encoded array of bytes.
      Parameters:
      v - input value
      Returns:
      two-byte big-endian representation
    • shortToBEBytes

      public static void shortToBEBytes(short v, byte[] output)
      Encode a 16-bit integer to big-endian bytes.
      Parameters:
      v - input value
      output - output buffer
    • shortToBEBytes

      public static void shortToBEBytes(short v, byte[] output, int offset)
      Encode a 16-bit integer to big-endian bytes.
      Parameters:
      v - input value
      output - output buffer
      offset - output offset
    • intToBEBytes

      public static byte[] intToBEBytes(int v)
      Convert a 32-bit integer to a big-endian encoded array of bytes.
      Parameters:
      v - input value
      Returns:
      four-byte big-endian representation
    • intToBEBytes

      public static void intToBEBytes(int v, byte[] output)
      Encode a 32-bit integer to big-endian bytes.
      Parameters:
      v - input value
      output - output buffer
    • intToBEBytes

      public static void intToBEBytes(int v, byte[] output, int offset)
      Encode a 32-bit integer to big-endian bytes.
      Parameters:
      v - input value
      output - output buffer
      offset - output offset
    • longToBEBytes

      public static byte[] longToBEBytes(long v)
      Convert a 64-bit integer to a big-endian encoded array of bytes.
      Parameters:
      v - input value
      Returns:
      eight-byte big-endian representation
    • longToBEBytes

      public static void longToBEBytes(long v, byte[] output)
      Encode a 64-bit integer to big-endian bytes.
      Parameters:
      v - input value
      output - output buffer
    • longToBEBytes

      public static void longToBEBytes(long v, byte[] output, int offset)
      Encode a 64-bit integer to big-endian bytes.
      Parameters:
      v - input value
      output - output buffer
      offset - output offset
    • littleEndianBytesToShort

      public static short littleEndianBytesToShort(byte[] array)
      Decode a little-endian 16-bit integer at offset 0.
      Parameters:
      array - input bytes
      Returns:
      decoded value
    • littleEndianBytesToShort

      public static short littleEndianBytesToShort(byte[] array, int offset)
      Decode a little-endian 16-bit integer.
      Parameters:
      array - input bytes
      offset - input offset
      Returns:
      decoded value
    • littleEndianBytesToInt

      public static int littleEndianBytesToInt(byte[] array)
      Decode a little-endian 32-bit integer at offset 0.
      Parameters:
      array - input bytes
      Returns:
      decoded value
    • littleEndianBytesToInt

      public static int littleEndianBytesToInt(byte[] array, int offset)
      Decode a little-endian 32-bit integer.
      Parameters:
      array - input bytes
      offset - input offset
      Returns:
      decoded value
    • littleEndianBytesToLong

      public static long littleEndianBytesToLong(byte[] array)
      Decode a little-endian 64-bit integer at offset 0.
      Parameters:
      array - input bytes
      Returns:
      decoded value
    • littleEndianBytesToLong

      public static long littleEndianBytesToLong(byte[] array, int offset)
      Decode a little-endian 64-bit integer.
      Parameters:
      array - input bytes
      offset - input offset
      Returns:
      decoded value
    • bigEndianBytesToShort

      public static short bigEndianBytesToShort(byte[] array)
      Decode a big-endian 16-bit integer at offset 0.
      Parameters:
      array - input bytes
      Returns:
      decoded value
    • bigEndianBytesToShort

      public static short bigEndianBytesToShort(byte[] array, int offset)
      Decode a big-endian 16-bit integer.
      Parameters:
      array - input bytes
      offset - input offset
      Returns:
      decoded value
    • bigEndianBytesToInt

      public static int bigEndianBytesToInt(byte[] array)
      Decode a big-endian 32-bit integer at offset 0.
      Parameters:
      array - input bytes
      Returns:
      decoded value
    • bigEndianBytesToInt

      public static int bigEndianBytesToInt(byte[] array, int offset)
      Decode a big-endian 32-bit integer.
      Parameters:
      array - input bytes
      offset - input offset
      Returns:
      decoded value
    • bigEndianBytesToLong

      public static long bigEndianBytesToLong(byte[] array)
      Decode a big-endian 64-bit integer at offset 0.
      Parameters:
      array - input bytes
      Returns:
      decoded value
    • bigEndianBytesToLong

      public static long bigEndianBytesToLong(byte[] array, int offset)
      Decode a big-endian 64-bit integer.
      Parameters:
      array - input bytes
      offset - input offset
      Returns:
      decoded value
    • numberToBytes

      public static void numberToBytes(ByteOrder bo, long v, byte[] output)
      Automatically convert a value depending on the byte array size. If size if not a pow of two, the floor pow of two is filled (for example is size is 5, value will be considered as integer). byte[] of more than 8 bytes will be cropped. This function is a bridge to xxxToLEBytes and xxxToBEBytes functions.
      Parameters:
      bo - byte order to use for output value
      v - input value
      output - output representation of v, using the given ByteOrder
    • bytesToNumberSigned

      public static long bytesToNumberSigned(ByteOrder bo, byte[] b)
      Convert a byte array to a number, sign is extended to long, meaning that {0x88} will result in 0xFFFFFFFF_FFFFFF88L
      Parameters:
      bo - byte order to use for decoding
      b - input bytes
      Returns:
      decoded signed value
    • bytesToNumberUnsigned

      public static long bytesToNumberUnsigned(ByteOrder bo, byte[] b)
      Convert a byte array to a number, zero extended to long, meaning that {0x88} will result in 0x88L
      Parameters:
      bo - byte order to use for decoding
      b - input bytes
      Returns:
      decoded unsigned value
    • bytesToShort

      public static short bytesToShort(byte[] array, int offset, ByteOrder order)
      Read 2 bytes and convert it to a short.
      Parameters:
      array - input buffer
      offset - buffer offset
      order - endianness
      Returns:
      decoded value
    • bytesToInt

      public static int bytesToInt(byte[] array, int offset, ByteOrder order)
      Read 4 bytes and convert it to an integer.
      Parameters:
      array - input buffer
      offset - buffer offset
      order - endianness
      Returns:
      decoded value
    • bytesToLong

      public static long bytesToLong(byte[] array, int offset, ByteOrder order)
      Read 8 bytes and convert it to a long.
      Parameters:
      array - input buffer
      offset - buffer offset
      order - endianness
      Returns:
      decoded value