Get a double from 8 bytes of the given array. - Java java.lang

Java examples for java.lang:byte Array to double

Description

Get a double from 8 bytes of the given array.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte[] b = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(bytes2double(b));
    }/*  ww  w  . j  ava  2s. c  o m*/

    /**
     * Get a double from 8 bytes of the given array.
     * 
     * @param b byte array.
     * @return a double.
     */
    public static double bytes2double(byte[] b) {
        return bytes2double(b, 0);
    }

    /**
     * Get a double from 8 bytes of the given array at specific offset.
     * 
     * @param b byte array.
     * @param off offset.
     * @return a double.
     */
    public static double bytes2double(byte[] b, int off) {
        long l = (((long) b[off]) << 56
                | ((long) (b[off + 1] & 0xff)) << 48
                | ((long) (b[off + 2]) & 0xff) << 40
                | ((long) (b[off + 3]) & 0xff) << 32
                | (((long) b[off + 4]) & 0xff) << 24
                | (((long) b[off + 5]) & 0xff) << 16
                | (((long) b[off + 6]) & 0xff) << 8 | (((long) b[off + 7]) & 0xff));
        return Double.longBitsToDouble(l);
    }
}

Related Tutorials