Android Byte Array to Double Convert getDouble(byte[] buf, boolean bigEndian)

Here you can find the source of getDouble(byte[] buf, boolean bigEndian)

Description

Convert byte sequence into java double from first 8 bytes

Parameter

Parameter Description
buf the source byte array
bigEndian true if big endian, false if little endian

Return

short the java double

Declaration

public static final double getDouble(byte[] buf, boolean bigEndian) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from  w w w. java  2 s  .  c  o m*/
     * Convert byte sequence into java double from first 8 bytes
     *
     * @param buf the source byte array
     * @param bigEndian true if big endian, false if little endian
     * @return short the java double
     */
    public static final double getDouble(byte[] buf, boolean bigEndian) {
        return Double.longBitsToDouble(getLong(buf, bigEndian));
    }

    /**
     * Convert byte sequence into java long from first 8 bytes
     *
     * @param buf the source byte array
     * @param bigEndian true if big endian, false if little endian
     * @return short the java long
     */
    public static final long getLong(byte[] buf, boolean bigEndian) {
        return getLong(buf, 0, bigEndian);
    }

    /**
     * Convert byte sequence into java long.
     *
     * @param buf the source byte array
     * @param pos the position of array to convert from
     * @param bigEndian true if big endian, false if little endian
     * @return short the java long
     */
    public static final long getLong(byte[] buf, int pos, boolean bigEndian) {
        if (bigEndian) {
            return (((long) buf[pos + 7]) & 0xFF)
                    | ((((long) buf[pos + 6]) & 0xFF) << 8)
                    | ((((long) buf[pos + 5]) & 0xFF) << 16)
                    | ((((long) buf[pos + 4]) & 0xFF) << 24)
                    | ((((long) buf[pos + 3]) & 0xFF) << 32)
                    | ((((long) buf[pos + 2]) & 0xFF) << 40)
                    | ((((long) buf[pos + 1]) & 0xFF) << 48)
                    | ((((long) buf[pos]) & 0xFF) << 56);
        } else {
            return (((long) buf[pos]) & 0xFF)
                    | ((((long) buf[pos + 1]) & 0xFF) << 8)
                    | ((((long) buf[pos + 2]) & 0xFF) << 16)
                    | ((((long) buf[pos + 3]) & 0xFF) << 24)
                    | ((((long) buf[pos + 4]) & 0xFF) << 32)
                    | ((((long) buf[pos + 5]) & 0xFF) << 40)
                    | ((((long) buf[pos + 6]) & 0xFF) << 48)
                    | ((((long) buf[pos + 7]) & 0xFF) << 56);
        }
    }
}

Related

  1. bytesBE2double(byte[] b, int off)
  2. bytesBE2doubles(byte[] b)
  3. bytesLE2double(byte[] b, int off)
  4. bytesLE2doubles(byte[] b)
  5. getDouble(byte[] b, int index)
  6. getDouble(byte[] bytes)
  7. toDouble(byte[] b, int pos)
  8. toDouble(byte[] b, int pos, int width)
  9. byteToDouble(byte[] b)