Class ByteConversionUtil


  • public class ByteConversionUtil
    extends java.lang.Object
    Conversion to/from byte arrays to/from short, int, long. The assumption is that they byte arrays are in network byte order (i.e., big-endian ordered).
    Author:
    kevin.w.wall@gmail.com
    See Also:
    CipherTextSerializer
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static byte[] fromInt​(int input)
      Returns a byte array containing 4 network byte-ordered bytes representing the given int.
      static byte[] fromLong​(long input)
      Returns a byte array containing 8 network byte-ordered bytes representing the given long.
      static byte[] fromShort​(short input)
      Returns a byte array containing 2 network byte ordered bytes representing the given short.
      static int toInt​(byte[] input)
      Converts a given byte array to an int.
      static long toLong​(byte[] input)
      Converts a given byte array to a long.
      static short toShort​(byte[] input)
      Converts a given byte array to an short.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ByteConversionUtil

        public ByteConversionUtil()
    • Method Detail

      • fromShort

        public static byte[] fromShort​(short input)
        Returns a byte array containing 2 network byte ordered bytes representing the given short.
        Parameters:
        input - An short to convert to a byte array.
        Returns:
        A byte array representation of an short in network byte order (i.e., big-endian order).
      • fromInt

        public static byte[] fromInt​(int input)
        Returns a byte array containing 4 network byte-ordered bytes representing the given int.
        Parameters:
        input - An int to convert to a byte array.
        Returns:
        A byte array representation of an int in network byte order (i.e., big-endian order).
      • fromLong

        public static byte[] fromLong​(long input)
        Returns a byte array containing 8 network byte-ordered bytes representing the given long.
        Parameters:
        input - The long to convert to a byte array.
        Returns:
        A byte array representation of a long.
      • toShort

        public static short toShort​(byte[] input)
        Converts a given byte array to an short. Bytes are expected in network byte order.
        Parameters:
        input - A network byte-ordered representation of an short, so exactly 2 bytes are expected.
        Returns:
        The short value represented by the input array.
      • toInt

        public static int toInt​(byte[] input)
        Converts a given byte array to an int. Bytes are expected in network byte order.
        Parameters:
        input - A network byte-ordered representation of an int. Must be exactly 4 bytes.
        Returns:
        The int value represented by the input array.
      • toLong

        public static long toLong​(byte[] input)
        Converts a given byte array to a long. Bytes are expected in network byte
        Parameters:
        input - A network byte-ordered representation of a long. Must be exactly 8 bytes.
        Returns:
        The long value represented by the input array