java.lang.Object
org.bouncycastle.util.Pack

public abstract class Packextends Object
Utility methods for converting byte arrays into ints and longs, and back again.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor for subclasses; the packing helpers in this base class are static.
  • Method Summary

    All Methods
    Modifier and Type
    Method
    Description
    static int
    bigEndianToInt(byte[] bs, int off)
    Convert a big-endian byte array to an int.
    static void
    bigEndianToInt(byte[] bs, int off, int[] ns)
    Convert a big-endian byte array to an array of ints.
    static void
    bigEndianToInt(byte[] bs, int off, int[] ns, int nsOff, int nsLen)
    Convert a big-endian byte array to a range of an int array.
    static long
    bigEndianToLong(byte[] bs, int off)
    Convert a big-endian byte array to a long.
    static long
    bigEndianToLong(byte[] bs, int off, int len)
    Convert a big-endian byte array of variable length to a long.
    static void
    bigEndianToLong(byte[] bs, int off, long[] ns)
    Convert a big-endian byte array to a long array.
    static void
    bigEndianToLong(byte[] bs, int bsOff, long[] ns, int nsOff, int nsLen)
    Convert a big-endian byte array to a range of a long array.
    static short
    bigEndianToShort(byte[] bs, int off)
    Convert a big-endian byte array to a short.
    static byte[]
    Convert an int to a big-endian byte array.
    static byte[]
    intToBigEndian(int[] ns)
    Convert an int array to a big-endian byte array.
    static void
    intToBigEndian(int[] ns, byte[] bs, int off)
    Write an int array to a byte array in big-endian order.
    static void
    intToBigEndian(int[] ns, int nsOff, int nsLen, byte[] bs, int bsOff)
    Write a range of an int array to a byte array in big-endian order.
    static void
    intToBigEndian(int n, byte[] bs, int off)
    Write an int to a byte array in big-endian order.
    static byte[]
    Convert an int to a little-endian byte array.
    static byte[]
    Convert an int array to a little-endian byte array.
    static void
    intToLittleEndian(int[] ns, byte[] bs, int off)
    Write an int array to a byte array in little-endian order.
    static void
    intToLittleEndian(int[] ns, int nsOff, int nsLen, byte[] bs, int bsOff)
    Write a range of an int array to a byte array in little-endian order.
    static void
    intToLittleEndian(int n, byte[] bs, int off)
    Write an int to a byte array in little-endian order.
    static int
    littleEndianToInt(byte[] bs, int off)
    Convert a little-endian byte array to an int.
    static int[]
    littleEndianToInt(byte[] bs, int off, int count)
    Convert a little-endian byte array to a new int array of specified count.
    static void
    littleEndianToInt(byte[] bs, int off, int[] ns)
    Convert a little-endian byte array to an int array.
    static void
    littleEndianToInt(byte[] bs, int bOff, int[] ns, int nOff, int count)
    Convert a little-endian byte array to a range of an int array.
    static int
    littleEndianToInt_High(byte[] bs, int off, int len)
    Convert a little-endian byte array of variable length to the high-order bits of an int.
    static int
    littleEndianToInt_Low(byte[] bs, int off, int len)
    Convert a little-endian byte array of variable length to an int (low-order bits).
    static long
    littleEndianToLong(byte[] bs, int off)
    Convert a little-endian byte array to a long.
    static long
    littleEndianToLong(byte[] input, int off, int len)
    Convert a little-endian byte array of variable length to a long.
    static void
    littleEndianToLong(byte[] bs, int off, long[] ns)
    Convert a little-endian byte array to a long array.
    static void
    littleEndianToLong(byte[] bs, int bsOff, long[] ns, int nsOff, int nsLen)
    Convert a little-endian byte array to a range of a long array.
    static long
    littleEndianToLong_High(byte[] bs, int off, int len)
    Convert a little-endian byte array of variable length to the high-order bits of a long.
    static long
    littleEndianToLong_Low(byte[] bs, int off, int len)
    Convert a little-endian byte array of variable length to a long (low-order bits).
    static short
    littleEndianToShort(byte[] bs, int off)
    Convert a little-endian byte array to a short.
    static byte[]
    Convert a long to a big-endian byte array.
    static byte[]
    longToBigEndian(long[] ns)
    Convert a long array to a big-endian byte array.
    static void
    longToBigEndian(long[] ns, byte[] bs, int off)
    Write a long array to a byte array in big-endian order.
    static void
    longToBigEndian(long[] ns, int nsOff, int nsLen, byte[] bs, int bsOff)
    Write a range of a long array to a byte array in big-endian order.
    static void
    longToBigEndian(long n, byte[] bs, int off)
    Write a long to a byte array in big-endian order.
    static byte[]
    Convert a long to a little-endian byte array.
    static byte[]
    longToLittleEndian(long[] ns)
    Convert a long array to a little-endian byte array.
    static void
    longToLittleEndian(long[] ns, byte[] bs, int off)
    Write a long array to a byte array in little-endian order.
    static void
    longToLittleEndian(long[] ns, int nsOff, int nsLen, byte[] bs, int bsOff)
    Write a range of a long array to a byte array in little-endian order.
    static void
    longToLittleEndian(long n, byte[] bs, int off)
    Write a long to a byte array in little-endian order.
    static void
    longToLittleEndian(long n, byte[] bs, int off, int len)
    Write a long to a byte array in little-endian order with variable length.
    static void
    longToLittleEndian_High(long n, byte[] bs, int off, int len)
    Write the high-order bytes of a long to a byte array in little-endian order.
    static byte[]
    Convert a short to a big-endian byte array.
    static void
    shortToBigEndian(short n, byte[] bs, int off)
    Write a short to a byte array in big-endian order.
    static byte[]
    Convert a short to a little-endian byte array.
    static void
    shortToLittleEndian(short n, byte[] bs, int off)
    Write a short to a byte array in little-endian order.

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Pack

      public Pack()
      Constructor for subclasses; the packing helpers in this base class are static.
  • Method Details

    • bigEndianToShort

      public static short bigEndianToShort(byte[] bs, int off)
      Convert a big-endian byte array to a short.
      Parameters:
      bs - the byte array
      off - the offset into the array
      Returns:
      the short value
    • bigEndianToInt

      public static int bigEndianToInt(byte[] bs, int off)
      Convert a big-endian byte array to an int.
      Parameters:
      bs - the byte array
      off - the offset into the array
      Returns:
      the int value
    • bigEndianToInt

      public static void bigEndianToInt(byte[] bs, int off, int[] ns)
      Convert a big-endian byte array to an array of ints.
      Parameters:
      bs - the byte array
      off - the offset into the array
      ns - the output int array
    • bigEndianToInt

      public static void bigEndianToInt(byte[] bs, int off, int[] ns, int nsOff, int nsLen)
      Convert a big-endian byte array to a range of an int array.
      Parameters:
      bs - the byte array
      off - the offset into the array
      ns - the output int array
      nsOff - the offset into the output array
      nsLen - the number of ints to write
    • intToBigEndian

      public static byte[] intToBigEndian(int n)
      Convert an int to a big-endian byte array.
      Parameters:
      n - the int value
      Returns:
      the byte array
    • intToBigEndian

      public static void intToBigEndian(int n, byte[] bs, int off)
      Write an int to a byte array in big-endian order.
      Parameters:
      n - the int value
      bs - the target byte array
      off - the offset into the array
    • intToBigEndian

      public static byte[] intToBigEndian(int[] ns)
      Convert an int array to a big-endian byte array.
      Parameters:
      ns - the int array
      Returns:
      the byte array
    • intToBigEndian

      public static void intToBigEndian(int[] ns, byte[] bs, int off)
      Write an int array to a byte array in big-endian order.
      Parameters:
      ns - the int array
      bs - the target byte array
      off - the offset into the array
    • intToBigEndian

      public static void intToBigEndian(int[] ns, int nsOff, int nsLen, byte[] bs, int bsOff)
      Write a range of an int array to a byte array in big-endian order.
      Parameters:
      ns - the int array
      nsOff - the offset into the int array
      nsLen - the number of ints to write
      bs - the target byte array
      bsOff - the offset into the byte array
    • bigEndianToLong

      public static long bigEndianToLong(byte[] bs, int off)
      Convert a big-endian byte array to a long.
      Parameters:
      bs - the byte array
      off - the offset into the array
      Returns:
      the long value
    • bigEndianToLong

      public static void bigEndianToLong(byte[] bs, int off, long[] ns)
      Convert a big-endian byte array to a long array.
      Parameters:
      bs - the byte array
      off - the offset into the array
      ns - the output long array
    • bigEndianToLong

      public static void bigEndianToLong(byte[] bs, int bsOff, long[] ns, int nsOff, int nsLen)
      Convert a big-endian byte array to a range of a long array.
      Parameters:
      bs - the byte array
      bsOff - the offset into the byte array
      ns - the output long array
      nsOff - the offset into the output array
      nsLen - the number of longs to write
    • bigEndianToLong

      public static long bigEndianToLong(byte[] bs, int off, int len)
      Convert a big-endian byte array of variable length to a long.
      Parameters:
      bs - the byte array
      off - the offset into the array
      len - the number of bytes to read (1-8)
      Returns:
      the long value
    • longToBigEndian

      public static byte[] longToBigEndian(long n)
      Convert a long to a big-endian byte array.
      Parameters:
      n - the long value
      Returns:
      the byte array
    • longToBigEndian

      public static void longToBigEndian(long n, byte[] bs, int off)
      Write a long to a byte array in big-endian order.
      Parameters:
      n - the long value
      bs - the target byte array
      off - the offset into the array
    • longToBigEndian

      public static byte[] longToBigEndian(long[] ns)
      Convert a long array to a big-endian byte array.
      Parameters:
      ns - the long array
      Returns:
      the byte array
    • longToBigEndian

      public static void longToBigEndian(long[] ns, byte[] bs, int off)
      Write a long array to a byte array in big-endian order.
      Parameters:
      ns - the long array
      bs - the target byte array
      off - the offset into the array
    • longToBigEndian

      public static void longToBigEndian(long[] ns, int nsOff, int nsLen, byte[] bs, int bsOff)
      Write a range of a long array to a byte array in big-endian order.
      Parameters:
      ns - the long array
      nsOff - the offset into the long array
      nsLen - the number of longs to write
      bs - the target byte array
      bsOff - the offset into the byte array
    • littleEndianToShort

      public static short littleEndianToShort(byte[] bs, int off)
      Convert a little-endian byte array to a short.
      Parameters:
      bs - the byte array
      off - the offset into the array
      Returns:
      the short value
    • littleEndianToInt

      public static int littleEndianToInt(byte[] bs, int off)
      Convert a little-endian byte array to an int.
      Parameters:
      bs - the byte array
      off - the offset into the array
      Returns:
      the int value
    • littleEndianToInt_High

      public static int littleEndianToInt_High(byte[] bs, int off, int len)
      Convert a little-endian byte array of variable length to the high-order bits of an int.
      Parameters:
      bs - the byte array
      off - the offset into the array
      len - the number of bytes to read (1-4)
      Returns:
      the int value shifted into the high-order bits
    • littleEndianToInt_Low

      public static int littleEndianToInt_Low(byte[] bs, int off, int len)
      Convert a little-endian byte array of variable length to an int (low-order bits).
      Parameters:
      bs - the byte array
      off - the offset into the array
      len - the number of bytes to read (1-4)
      Returns:
      the int value
    • littleEndianToInt

      public static void littleEndianToInt(byte[] bs, int off, int[] ns)
      Convert a little-endian byte array to an int array.
      Parameters:
      bs - the byte array
      off - the offset into the array
      ns - the output int array
    • littleEndianToInt

      public static void littleEndianToInt(byte[] bs, int bOff, int[] ns, int nOff, int count)
      Convert a little-endian byte array to a range of an int array.
      Parameters:
      bs - the byte array
      bOff - the offset into the byte array
      ns - the output int array
      nOff - the offset into the output array
      count - the number of ints to write
    • littleEndianToInt

      public static int[] littleEndianToInt(byte[] bs, int off, int count)
      Convert a little-endian byte array to a new int array of specified count.
      Parameters:
      bs - the byte array
      off - the offset into the array
      count - the number of ints to read
      Returns:
      the int array
    • shortToLittleEndian

      public static byte[] shortToLittleEndian(short n)
      Convert a short to a little-endian byte array.
      Parameters:
      n - the short value
      Returns:
      the byte array
    • shortToLittleEndian

      public static void shortToLittleEndian(short n, byte[] bs, int off)
      Write a short to a byte array in little-endian order.
      Parameters:
      n - the short value
      bs - the target byte array
      off - the offset into the array
    • shortToBigEndian

      public static byte[] shortToBigEndian(short n)
      Convert a short to a big-endian byte array.
      Parameters:
      n - the short value
      Returns:
      the byte array
    • shortToBigEndian

      public static void shortToBigEndian(short n, byte[] bs, int off)
      Write a short to a byte array in big-endian order.
      Parameters:
      n - the short value
      bs - the target byte array
      off - the offset into the array
    • intToLittleEndian

      public static byte[] intToLittleEndian(int n)
      Convert an int to a little-endian byte array.
      Parameters:
      n - the int value
      Returns:
      the byte array
    • intToLittleEndian

      public static void intToLittleEndian(int n, byte[] bs, int off)
      Write an int to a byte array in little-endian order.
      Parameters:
      n - the int value
      bs - the target byte array
      off - the offset into the array
    • intToLittleEndian

      public static byte[] intToLittleEndian(int[] ns)
      Convert an int array to a little-endian byte array.
      Parameters:
      ns - the int array
      Returns:
      the byte array
    • intToLittleEndian

      public static void intToLittleEndian(int[] ns, byte[] bs, int off)
      Write an int array to a byte array in little-endian order.
      Parameters:
      ns - the int array
      bs - the target byte array
      off - the offset into the array
    • intToLittleEndian

      public static void intToLittleEndian(int[] ns, int nsOff, int nsLen, byte[] bs, int bsOff)
      Write a range of an int array to a byte array in little-endian order.
      Parameters:
      ns - the int array
      nsOff - the offset into the int array
      nsLen - the number of ints to write
      bs - the target byte array
      bsOff - the offset into the byte array
    • littleEndianToLong

      public static long littleEndianToLong(byte[] bs, int off)
      Convert a little-endian byte array to a long.
      Parameters:
      bs - the byte array
      off - the offset into the array
      Returns:
      the long value
    • littleEndianToLong

      public static void littleEndianToLong(byte[] bs, int off, long[] ns)
      Convert a little-endian byte array to a long array.
      Parameters:
      bs - the byte array
      off - the offset into the array
      ns - the output long array
    • littleEndianToLong

      public static long littleEndianToLong(byte[] input, int off, int len)
      Convert a little-endian byte array of variable length to a long.
      Parameters:
      input - the byte array
      off - the offset into the array
      len - the number of bytes to read (1-8)
      Returns:
      the long value
    • littleEndianToLong

      public static void littleEndianToLong(byte[] bs, int bsOff, long[] ns, int nsOff, int nsLen)
      Convert a little-endian byte array to a range of a long array.
      Parameters:
      bs - the byte array
      bsOff - the offset into the byte array
      ns - the output long array
      nsOff - the offset into the output array
      nsLen - the number of longs to write
    • longToLittleEndian_High

      public static void longToLittleEndian_High(long n, byte[] bs, int off, int len)
      Write the high-order bytes of a long to a byte array in little-endian order.
      Parameters:
      n - the long value
      bs - the target byte array
      off - the offset into the array
      len - the number of bytes to write (1-8)
    • longToLittleEndian

      public static void longToLittleEndian(long n, byte[] bs, int off, int len)
      Write a long to a byte array in little-endian order with variable length.
      Parameters:
      n - the long value
      bs - the target byte array
      off - the offset into the array
      len - the number of bytes to write (1-8)
    • littleEndianToLong_High

      public static long littleEndianToLong_High(byte[] bs, int off, int len)
      Convert a little-endian byte array of variable length to the high-order bits of a long.
      Parameters:
      bs - the byte array
      off - the offset into the array
      len - the number of bytes to read (1-8)
      Returns:
      the long value shifted into the high-order bits
    • littleEndianToLong_Low

      public static long littleEndianToLong_Low(byte[] bs, int off, int len)
      Convert a little-endian byte array of variable length to a long (low-order bits).
      Parameters:
      bs - the byte array
      off - the offset into the array
      len - the number of bytes to read (1-8)
      Returns:
      the long value
    • longToLittleEndian

      public static byte[] longToLittleEndian(long n)
      Convert a long to a little-endian byte array.
      Parameters:
      n - the long value
      Returns:
      the byte array
    • longToLittleEndian

      public static void longToLittleEndian(long n, byte[] bs, int off)
      Write a long to a byte array in little-endian order.
      Parameters:
      n - the long value
      bs - the target byte array
      off - the offset into the array
    • longToLittleEndian

      public static byte[] longToLittleEndian(long[] ns)
      Convert a long array to a little-endian byte array.
      Parameters:
      ns - the long array
      Returns:
      the byte array
    • longToLittleEndian

      public static void longToLittleEndian(long[] ns, byte[] bs, int off)
      Write a long array to a byte array in little-endian order.
      Parameters:
      ns - the long array
      bs - the target byte array
      off - the offset into the array
    • longToLittleEndian

      public static void longToLittleEndian(long[] ns, int nsOff, int nsLen, byte[] bs, int bsOff)
      Write a range of a long array to a byte array in little-endian order.
      Parameters:
      ns - the long array
      nsOff - the offset into the long array
      nsLen - the number of longs to write
      bs - the target byte array
      bsOff - the offset into the byte array