Java Double Number Create toDouble(byte[] si, boolean isReverseOrder)

Here you can find the source of toDouble(byte[] si, boolean isReverseOrder)

Description

Convert a byte[] to the corresponding double value.

License

LGPL

Parameter

Parameter Description
si the input array
isReverseOrder True if llittle-endian. False if big-endian (Most significant byte first)

Return

The value coresponding to the byte array

Declaration

public final static double toDouble(byte[] si, boolean isReverseOrder) 

Method Source Code

//package com.java2s;
/*//from  w  ww.  ja  v a2 s.  c  o  m
 * Copyright 2005 MBARI
 *
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 2.1 
 * (the "License"); you may not use this file except in compliance 
 * with the License. You may obtain a copy of the License at
 *
 * http://www.gnu.org/copyleft/lesser.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Convert a byte[] (most significant byte first) to the corresponding <b>double</b> value.
     *
     * @param  si  the input array
     * @return     The value coresponding to the byte array
     */
    public final static double toDouble(byte[] si) {
        return toDouble(si, false);
    }

    /**
     * Convert a byte[] to the corresponding <b>double</b> value.
     *
     * @param  si              the input array
     * @param  isReverseOrder  True if llittle-endian. False if big-endian (Most significant byte first)
     * @return                 The value coresponding to the byte array
     */
    public final static double toDouble(byte[] si, boolean isReverseOrder) {
        double d = 0.0;
        // Double var_double = new Double(d);
        long l = toLong(si, isReverseOrder);
        d = Double.longBitsToDouble(l);
        return d;
    }

    /**
     * Convert a byte[] (most significant byte first) to the corresponding <b>long</b> value.
     *
     * @param  si  the input array
     * @return     The value coresponding to the byte array
     */
    public final static long toLong(byte[] si) {
        return toLong(si, false);
    }

    /**
     * Convert a byte[] to the corresponding <b>long</b> value.
     *
     * @param  si              the input array
     * @param  isReverseOrder  True if llittle-endian. False if big-endian (Most significant byte first)
     * @return                 The value coresponding to the byte array
     */
    public final static long toLong(byte[] si, boolean isReverseOrder) {
        long l = 0L;
        if (isReverseOrder) {
            si = reverseOrder(si, 8);
        }

        // for (byte i = 0; i <= 7; i++) {
        int nb = si.length - 1;
        for (byte i = 0; i <= nb; i++) {
            long j;
            if (si[i] < 0) {
                si[i] = (byte) (si[i] & 0x7f);
                j = (long) si[i];
                j |= 0x80L;
            } else {
                j = (long) si[i];
            }

            l |= j;

            if (i < nb) {
                l <<= 8;
            }
        }

        return l;
    }

    /**
     * Reverse the ordering of a byte array
     *
     * @param  si
     * @param  i
     * @return
     */
    private final static byte[] reverseOrder(byte[] si, int i) {
        byte[] is = new byte[i];
        for (byte b = 0; b <= i - 1; b++) {
            is[b] = si[i - 1 - b];
        }

        return is;
    }
}

Related

  1. toDouble(byte[] bytes, int offset)
  2. toDouble(byte[] data)
  3. toDouble(byte[] data)
  4. toDouble(byte[] data)
  5. toDouble(byte[] data, int offset)
  6. toDouble(byte[] value)
  7. toDouble(final byte[] array, final int offset, final int length)
  8. toDouble(final byte[] bytes)
  9. toDouble(final byte[] bytes)