Gets the IEEE value from the byte array passed in - Java java.lang

Java examples for java.lang:double Array

Description

Gets the IEEE value from the byte array passed in

Demo Code

/*/*from w  w w .  j  a  v  a  2 s .  c o m*/
 * Copyright (c) 2007-2016 AREasy Runtime
 *
 * This library, AREasy Runtime and API for BMC Remedy AR System, is free software ("Licensed Software");
 * you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either version 2.1 of the License,
 * or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * including but not limited to, the implied warranty of MERCHANTABILITY, NONINFRINGEMENT,
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 */

public class Main{
    /**
     * Gets the IEEE value from the byte array passed in
     *
     * @param pos  the position in the data block which contains the double value
     * @param data the data block containing the raw bytes
     * @return the double value converted from the raw data
     */
    public static double getIEEEDouble(byte[] data, int pos) {
        int num1 = IntegerHelper.getInt(data[pos], data[pos + 1],
                data[pos + 2], data[pos + 3]);
        int num2 = IntegerHelper.getInt(data[pos + 4], data[pos + 5],
                data[pos + 6], data[pos + 7]);

        // Long.parseLong doesn't like the sign bit, so have to extract this
        // information and put it in at the end.  (Acknowledgment:  thanks
        // to Ruben for pointing this out)
        boolean negative = ((num2 & 0x80000000) != 0);

        // Thanks to Lyle for the following improved IEEE double processing
        long val = ((num2 & 0x7fffffff) * 0x100000000L)
                + (num1 < 0 ? 0x100000000L + num1 : num1);
        double value = Double.longBitsToDouble(val);

        if (negative) {
            value = -value;
        }
        return value;
    }
}

Related Tutorials