Class NumberUtils


  • @ThreadSafe
    public class NumberUtils
    extends java.lang.Object
    A number utility class with various useful operations on numbers and number arrays.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  NumberUtils.Strategy
      Strategy defining how to manipulate with bytes.
    • Constructor Summary

      Constructors 
      Constructor Description
      NumberUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int bcd2bin​(int bcd)
      Converts packed BCD code (1 byte, 2 BCD digits) to binary It is assumed the BCD has little endian.
      static int bin2bcd​(int bin)
      Converts a binary number into packed BCD (1 byte, 2 BCD digits)
      static int[] listToNativeInts​(java.util.List<java.lang.Integer> list)
      Converts list of Integers into array of native ints.
      static java.lang.Byte[] nativeBytesToBytes​(byte[] array)
      Converts native byte[] array to boxed Byte[] array.
      static java.lang.Integer[] nativeBytesToIntegers​(byte[] array)
      Converts native byte[] array to boxed Integer[] array.
      static int[] nativeBytesToInts​(byte[] array)
      Converts native byte[] array to boxed Integer[] array.
      static java.lang.Short[] nativeBytesToShorts​(byte[] array)
      Converts native byte[] array to boxed Short[] array.
      static byte[] nativeIntsToNativeBytes​(int[] numbers)
      Converts native short[] array to native byte[] array.
      static java.lang.Byte[] nativeShortsToBytes​(short[] numbers)
      Converts native short[] array to Byte[] array.
      static byte[] nativeShortsToNativeBytes​(short[] numbers)
      Converts native short[] array to native byte[] array.
      static java.lang.Short[] nativeShortsToShorts​(short[] numbers)
      Converts native short[] array to Short[] array.
      static java.lang.Byte[] numbersToBytes​(java.lang.Number[] numbers)
      Converts Number[] array to Byte[] array.
      static byte[] numbersToNativeBytes​(java.lang.Number[] numbers)
      Converts Number[] array to native byte[] array.
      static int readBits​(byte[] bytes, int start, int length, int bytesStrategy)
      Reads an arbitrary number of bits from bytes.
      static int readInt​(byte[] word, int strategy)
      Reads an integer from the array of numbers.
      static int readInt​(byte[] word, int startOffset, int length, int strategy)
      Reads an integer from the array of numbers.
      static int readInt​(int[] word, int strategy)
      Reads an integer from the array of numbers.
      static int readInt​(java.lang.Byte[] word, int strategy)
      Reads an integer from the array of numbers.
      static int readInt​(java.lang.Integer[] word, int strategy)
      Reads an integer from the array of numbers.
      static int reverseBits​(int value, int numberOfBits)
      Reverse bits in integer (max 32-bit) value.
      static long reverseBits​(long value, int numberOfBits)
      Reverse bits in long (max 64-bit) value.
      static java.lang.Byte[] shortsToBytes​(java.lang.Short[] numbers)
      Converts Short[] array to Byte[] array.
      static byte[] shortsToNativeBytes​(java.lang.Short[] numbers)
      Converts Short[] array to native byte[] array.
      static short[] shortsToNativeShorts​(java.lang.Short[] numbers)
      Converts Short[] array to native short[] array.
      static void writeInt​(int value, byte[] output, int strategy)
      Split the value into 4 bytes.
      static void writeInt​(int value, int[] output, int strategy)
      Split the value into 4 bytes.
      static void writeInt​(int value, java.lang.Byte[] output, int strategy)
      Split the value into 4 bytes.
      static void writeInt​(int value, java.lang.Integer[] output, int strategy)
      Split the value into 4 bytes.
      static void writeInt​(int value, java.lang.Short[] output, int strategy)
      Split the value into 4 bytes.
      • Methods inherited from class java.lang.Object

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

      • NumberUtils

        public NumberUtils()
    • Method Detail

      • reverseBits

        public static int reverseBits​(int value,
                                      int numberOfBits)
        Reverse bits in integer (max 32-bit) value.
        Parameters:
        value - value which bits will be reversed
        numberOfBits - how many bits should be reversed. If the value has more bits, the rest will be preserved.
        Returns:
        value with reversed bits
      • reverseBits

        public static long reverseBits​(long value,
                                       int numberOfBits)
        Reverse bits in long (max 64-bit) value.
        Parameters:
        value - value which bits will be reversed
        numberOfBits - how many bits should be reversed. If the value has more bits, the rest will be ignored.
        Returns:
        value with reversed bits
      • readBits

        public static int readBits​(byte[] bytes,
                                   int start,
                                   int length,
                                   int bytesStrategy)
        Reads an arbitrary number of bits from bytes. If bytesStrategy is LITTLE_ENDIAN, bits are read from LSB to MSB. If the strategy is BIG_ENDIAN, bits are read from MSB to LSB.
        Parameters:
        bytes - bytes
        start - the number of bits from the start of the current instruction
        length - the number of bits to read
        bytesStrategy - strategy for how to read bytes
        Returns:
        the bytes read
      • readInt

        public static int readInt​(java.lang.Byte[] word,
                                  int strategy)
        Reads an integer from the array of numbers.

        Uses ByteBuffer.wrap. The array must have 4 items - because integer has 4 bytes.

        Parameters:
        word - the array of 4 bytes
        strategy - strategy how to deal with the array. See Strategy class for more information.
        Returns:
        Single integer number which combines the array of bytes into one 32-bit value
      • readInt

        public static int readInt​(byte[] word,
                                  int strategy)
        Reads an integer from the array of numbers.

        Uses ByteBuffer.wrap. The array must have exactly 4 items - because integer has 4 bytes. If the array has more bytes, they are ignored. Which ones are ignored depends on the byte ordering (=strategy).

        Parameters:
        word - the array of 4 bytes
        strategy - strategy how to deal with the array. See Strategy class for more information.
        Returns:
        Single integer number which combines the array of bytes into one 32-bit value
      • readInt

        public static int readInt​(byte[] word,
                                  int startOffset,
                                  int length,
                                  int strategy)
        Reads an integer from the array of numbers.

        Uses ByteBuffer.wrap. The array should have up to 4 items, each one represents a byte. If the length is less than 4, the array for reading is padded with zeroes from the left (in case of big endian) or from the right (in case of little endian), so that the array size is 4.

        Parameters:
        word - the array of 4 bytes
        startOffset - starting offset in the array
        length - length in the array
        strategy - strategy how to deal with the array. See Strategy class for more information.
        Returns:
        Single integer number which combines the array of bytes into one 32-bit value
      • readInt

        public static int readInt​(java.lang.Integer[] word,
                                  int strategy)
        Reads an integer from the array of numbers.

        Uses ByteBuffer.wrap. The array must have 4 items, each one must represent a byte. If the value in the array is larger than a byte, the higher-order bits are cut.

        Parameters:
        word - the array of 4 bytes
        strategy - strategy how to deal with the array. See Strategy class for more information.
        Returns:
        Single integer number which combines the array of bytes into one 32-bit value
      • readInt

        public static int readInt​(int[] word,
                                  int strategy)
        Reads an integer from the array of numbers.

        Uses ByteBuffer.wrap. The array must have 4 items, each one must represent a byte. If the value in the array is larger than a byte, the higher-order bits are cut.

        Parameters:
        word - the array of 4 bytes
        strategy - strategy how to deal with the array. See Strategy class for more information.
        Returns:
        Single integer number which combines the array of bytes into one 32-bit value
      • writeInt

        public static void writeInt​(int value,
                                    java.lang.Integer[] output,
                                    int strategy)
        Split the value into 4 bytes.

        Uses ByteBuffer.

        Parameters:
        value - The value which should be split into bytes. It is assumed that it is always in native little endian.
        output - The output array. Must have space for 4 bytes. If the array is larger, other elements are ignored.
        strategy - strategy for how to save the value. See Strategy class for more information.
      • writeInt

        public static void writeInt​(int value,
                                    int[] output,
                                    int strategy)
        Split the value into 4 bytes.

        Uses ByteBuffer.

        Parameters:
        value - The value which should be split into bytes. It is assumed that it is always in native little endian.
        output - The output array. Must have space for 4 bytes. If the array is larger, other elements are ignored.
        strategy - strategy for how to save the value. See Strategy class for more information.
      • writeInt

        public static void writeInt​(int value,
                                    java.lang.Byte[] output,
                                    int strategy)
        Split the value into 4 bytes.

        Uses ByteBuffer.

        Parameters:
        value - The value which should be split into bytes. It is assumed that it is always in native little endian.
        output - The output array. Must have space for 4 bytes. If the array is larger, other elements are ignored.
        strategy - strategy for how to save the value. See Strategy class for more information.
      • writeInt

        public static void writeInt​(int value,
                                    byte[] output,
                                    int strategy)
        Split the value into 4 bytes.

        Uses ByteBuffer.

        Parameters:
        value - The value which should be split into bytes. It is assumed that it is always in native little endian.
        output - The output array. Must have space for 4 bytes. If the array is larger, other elements are ignored.
        strategy - strategy for how to save the value. See Strategy class for more information.
      • writeInt

        public static void writeInt​(int value,
                                    java.lang.Short[] output,
                                    int strategy)
        Split the value into 4 bytes.

        Uses binary arithmetic.

        Parameters:
        value - The value which should be split into bytes. It is assumed that it is always in native little endian.
        output - The output array. Must have space for 4 bytes. If the array is larger, other elements are ignored.
        strategy - strategy for how to save the value. See Strategy class for more information.
      • numbersToBytes

        public static java.lang.Byte[] numbersToBytes​(java.lang.Number[] numbers)
        Converts Number[] array to Byte[] array. Every number is converted to byte using number.byteValue() call.
        Parameters:
        numbers - numbers array
        Returns:
        boxed Byte[] array
      • numbersToNativeBytes

        public static byte[] numbersToNativeBytes​(java.lang.Number[] numbers)
        Converts Number[] array to native byte[] array. Every number is converted to byte using number.byteValue() call.
        Parameters:
        numbers - numbers array
        Returns:
        native byte[] array
      • shortsToBytes

        public static java.lang.Byte[] shortsToBytes​(java.lang.Short[] numbers)
        Converts Short[] array to Byte[] array. Every number is converted to byte using number.byteValue() call.
        Parameters:
        numbers - numbers array
        Returns:
        boxed Byte[] array
      • shortsToNativeBytes

        public static byte[] shortsToNativeBytes​(java.lang.Short[] numbers)
        Converts Short[] array to native byte[] array. Every number is converted to byte using number.byteValue() call.
        Parameters:
        numbers - numbers array
        Returns:
        native byte[] array
      • shortsToNativeShorts

        public static short[] shortsToNativeShorts​(java.lang.Short[] numbers)
        Converts Short[] array to native short[] array.
        Parameters:
        numbers - numbers array
        Returns:
        native short[] array
      • nativeShortsToNativeBytes

        public static byte[] nativeShortsToNativeBytes​(short[] numbers)
        Converts native short[] array to native byte[] array. Every number is converted to byte using number.byteValue() call.
        Parameters:
        numbers - numbers array
        Returns:
        native byte[] array
      • nativeIntsToNativeBytes

        public static byte[] nativeIntsToNativeBytes​(int[] numbers)
        Converts native short[] array to native byte[] array. Every number is converted to byte using number.byteValue() call.
        Parameters:
        numbers - numbers array
        Returns:
        native byte[] array
      • nativeShortsToBytes

        public static java.lang.Byte[] nativeShortsToBytes​(short[] numbers)
        Converts native short[] array to Byte[] array. Every number is converted to byte using number & 0xFF
        Parameters:
        numbers - numbers array
        Returns:
        boxed Byte[] array
      • nativeShortsToShorts

        public static java.lang.Short[] nativeShortsToShorts​(short[] numbers)
        Converts native short[] array to Short[] array. Every number is converted to byte using number.byteValue() call.
        Parameters:
        numbers - numbers array
        Returns:
        boxed Short[] array
      • nativeBytesToBytes

        public static java.lang.Byte[] nativeBytesToBytes​(byte[] array)
        Converts native byte[] array to boxed Byte[] array.
        Parameters:
        array - native byte[] array
        Returns:
        boxed Byte[] array
      • nativeBytesToIntegers

        public static java.lang.Integer[] nativeBytesToIntegers​(byte[] array)
        Converts native byte[] array to boxed Integer[] array.
        Parameters:
        array - native byte[] array
        Returns:
        boxed Integer[] array
      • nativeBytesToShorts

        public static java.lang.Short[] nativeBytesToShorts​(byte[] array)
        Converts native byte[] array to boxed Short[] array.
        Parameters:
        array - native byte[] array
        Returns:
        boxed Short[] array
      • nativeBytesToInts

        public static int[] nativeBytesToInts​(byte[] array)
        Converts native byte[] array to boxed Integer[] array.
        Parameters:
        array - native byte[] array
        Returns:
        boxed Integer[] array
      • listToNativeInts

        public static int[] listToNativeInts​(java.util.List<java.lang.Integer> list)
        Converts list of Integers into array of native ints.
        Parameters:
        list - list of integers
        Returns:
        native int[] array
      • bcd2bin

        public static int bcd2bin​(int bcd)
        Converts packed BCD code (1 byte, 2 BCD digits) to binary It is assumed the BCD has little endian.
        Parameters:
        bcd - packed BCD code
        Returns:
        binary number
      • bin2bcd

        public static int bin2bcd​(int bin)
        Converts a binary number into packed BCD (1 byte, 2 BCD digits)
        Parameters:
        bin - binary number
        Returns:
        number in packed BCD code, little endian