Class BinaryConversionUtils


  • public class BinaryConversionUtils
    extends Object
    • Constructor Detail

      • BinaryConversionUtils

        public BinaryConversionUtils()
    • Method Detail

      • convertMSBSigned

        public static int convertMSBSigned​(byte[] b,
                                           int n)
        Converts an array of bytes into an int. The array of bytes may be of length 1, 2, 3, or 4, in big-endian order. That is, b[0] holds the most significant bits, b[n-1] the least. The bytes are assumed to be signed.
        Parameters:
        b - the array of bytes, in big-endian order
        n - the number of bytes to convert in the array, from 1 to the size of an int
        Returns:
        the signed int value represented by the bytes
      • convertLSBSigned

        public static int convertLSBSigned​(byte[] b,
                                           int n)
        Converts an array of bytes into an int. The array of bytes may be of length 1, 2, 3, or 4, in little-endian order. That is, b[n-1] holds the most significant bits, b[n-1] the least. The bytes are assumed to be signed.
        Parameters:
        b - the array of bytes, in little-endian order
        n - the number of bytes to convert in the array, from 1 to the size of an int
        Returns:
        the signed int value represented by the bytes
      • convertMSBUnsigned

        public static long convertMSBUnsigned​(int[] b,
                                              int n)
        Converts an array of bytes into a long, permitting unsigned bytes. The array of bytes may be of length 1, 2, 3, or 4, in big-endian order. That is, b[0] holds the most significant bits, b[n-1] the least.
        Parameters:
        b - the array of bytes, in big-endian order
        n - the number of bytes to convert in the array, from 1 to the size of an int
        Returns:
        the signed int value represented by the bytes
      • convertMSBUnsigned

        public static long convertMSBUnsigned​(byte[] b,
                                              int n)
        Converts an array of bytes into a long, permitting unsigned bytes. The array of bytes may be of length 1, 2, 3, or 4, in big-endian order. That is, b[0] holds the most significant bits, b[n-1] the least.
        Parameters:
        b - the array of bytes, in big-endian order
        n - the number of bytes to convert in the array, from 1 to the size of an int
        Returns:
        the signed int value represented by the bytes
      • convertLSBUnsigned

        public static long convertLSBUnsigned​(int[] b,
                                              int n)
      • convertLSBUnsigned

        public static long convertLSBUnsigned​(byte[] b,
                                              int n)
      • MSBByteArrayToFloat

        public static float MSBByteArrayToFloat​(byte[] bytes)
        convert MSB byte array (of size 4) to float
        Parameters:
        test -
        Returns:
      • LSBByteArrayToFloat

        public static float LSBByteArrayToFloat​(byte[] bytes)
        convert LSB byte array (of size 4) to float
        Parameters:
        test -
        Returns:
      • byteArrayToString

        public static String byteArrayToString​(byte[] byteArray,
                                               String encoding)
        Convert byte array to String. Use the correct encoding, ASCII (7-bit) or EBCDIC (8-bit) In the case it is not able to use the correct encoding, display the bytes in decimal format as follows: [87,87,....]
        Parameters:
        byteArray -
        Returns:
      • bitToString

        public static void bitToString​(StringBuilder byteValue,
                                       boolean bit)
        Appends a logical 1 or 0 to the existing string based on the value of the bit Used to convert a byte array to string.
      • convertByteArrayToLong

        public static long convertByteArrayToLong​(byte[] b)
        Convert a byte array of size 8 bytes to type long
        Parameters:
        b - : byte array of size 8 bytes
        Returns:
        long representation of the byte array Note: Need to type cast to type long since cannot shift pass 31 bits
      • convertByteArrayToInt

        public static int convertByteArrayToInt​(byte[] b)
        Convert a byte array of size 4 bytes to type int
        Parameters:
        b - : byte array if size 4 bytes
        Returns:
        int representation of the byte array
      • vaxFTypeToIEEESingle

        public static int vaxFTypeToIEEESingle​(byte[] b)
        Convert from VAX F-type 4-byte to IEEE single precision
        Parameters:
        b - - byte array of size 4 bytes
        Returns:
        Byte array in integer format with all the bits ordered in IEEE-754 4 byte format
      • vaxDTypeToIEEEDouble

        public static long vaxDTypeToIEEEDouble​(byte[] b)
        Convert from VAX D-type to IEEE double precision. For compatibility, the mantissa is truncated from 55 bits to 52 bits
        Parameters:
        b - - byte array of size 8 bytes
        Returns:
        Byte array in long format with all the bits ordered in IEEE-754 8 byte format
      • vaxGTypeToIEEEDouble

        public static long vaxGTypeToIEEEDouble​(byte[] b)
        Convert from VAX G-type to IEEE 8-byte double precision
        Parameters:
        b - - byte array of size 8 bytes
        Returns:
        Byte array in long format with all the bits ordered in IEEE-754 8 byte format
      • pcReal4BTypeToIEEESingle

        public static int pcReal4BTypeToIEEESingle​(byte[] b)
        Convert from PC_REAL type to IEEE single precision
        Parameters:
        b - - byte array of size 4 bytes
        Returns:
        The byte array in integer format
      • pcReal8BTypeToIEEEDouble

        public static long pcReal8BTypeToIEEEDouble​(byte[] b)
        Convert from PC_REAL 8 Byte type to IEEE double precision
        Parameters:
        b - - byte array of size 8 bytes
        Returns:
        Byte array in long type