Java Byte Array to Double bytesToDouble(byte[] bytes, int startIndex)

Here you can find the source of bytesToDouble(byte[] bytes, int startIndex)

Description

Given an array of bytes, convert it to a double, least significant byte is stored in the beginning.

License

Open Source License

Parameter

Parameter Description
bytes the byte array
startIndex the starting index of the array where the long is stored.

Declaration

public static double bytesToDouble(byte[] bytes, int startIndex) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from  w  w w . ja  va 2  s  .co  m
     * Given an array of bytes, convert it to a double, least significant byte is stored in the beginning.
     * 
     * @param bytes
     *            the byte array
     * @param startIndex
     *            the starting index of the array where the long is stored.
     * @ret the double result.
     */
    public static double bytesToDouble(byte[] bytes, int startIndex) {
        return Double.longBitsToDouble(bytesToLong(bytes, startIndex));
    }

    /**
     * Given an array of bytes, convert it to a long, least significant byte is stored in the beginning.
     * 
     * @param bytes
     *            the byte array
     * @param startIndex
     *            the starting index of the array where the long is stored.
     * @ret the long result.
     */
    public static long bytesToLong(byte[] bytes, int startIndex) {
        // the lower 4 bytes
        // long temp = (long)bytesToInt(bytes, startIndex) & (long)0xffffffff;
        // return temp | ((long)bytesToInt(bytes, startIndex+4) << 32);

        return (((long) bytes[startIndex] & 0xff) | (((long) bytes[startIndex + 1] & 0xff) << 8)
                | (((long) bytes[startIndex + 2] & 0xff) << 16) | (((long) bytes[startIndex + 3] & 0xff) << 24)
                | (((long) bytes[startIndex + 4] & 0xff) << 32) | (((long) bytes[startIndex + 5] & 0xff) << 40)
                | (((long) bytes[startIndex + 6] & 0xff) << 48) | (((long) bytes[startIndex + 7] & 0xff) << 56));
    }
}

Related

  1. bytesToDouble(byte[] buffer)
  2. bytesToDouble(byte[] bytes)
  3. bytesToDouble(byte[] bytes)
  4. bytesToDouble(byte[] bytes, int off)
  5. bytesToDouble(byte[] bytes, int offset)
  6. bytesToDouble(byte[] data, int[] offset)
  7. bytesToDouble(final byte[] bytes)
  8. bytesToDoubles(byte[] b)
  9. byteToDouble(byte[] b)