Converts an array of bytes to a double. - Java java.lang

Java examples for java.lang:byte Array Convert

Description

Converts an array of bytes to a double.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte data = 2;
        System.out.println(bytesToDouble(data));
    }// w  w w  .  ja va2s  .co  m

    /**
     * <p>
     * Converts an array of bytes to a double. If there are more than eight bytes, the first eight bytes in the array are used and
     * the rest are ignored.<br>
     * There is no error checking done, so please ensure the byte array contains at least eight elements.
     * </p>
     * 
     * @param data
     *            The byte array to be converted to a double.
     * @return The double equivalent of the given byte array.
     */
    public static double bytesToDouble(byte... data) {
        return (Double.longBitsToDouble((data[7] & 0xFFL) << 56
                | (data[6] & 0xFFL) << 48 | (data[5] & 0xFFL) << 40
                | (data[4] & 0xFFL) << 32 | (data[3] & 0xFF) << 24
                | (data[2] & 0xFF) << 16 | (data[1] & 0xFF) << 8 | data[0]
                & 0xFF));
    }
}

Related Tutorials