Java Byte Array to Double bytesToDouble(byte[] buffer)

Here you can find the source of bytesToDouble(byte[] buffer)

Description

This function converts the bytes in a byte array to its corresponding double value.

License

Open Source License

Parameter

Parameter Description
buffer The byte array containing the double.

Return

The corresponding double value.

Declaration

static public double bytesToDouble(byte[] buffer) 

Method Source Code

//package com.java2s;
/************************************************************************
 * Copyright (c) Crater Dog Technologies(TM).  All Rights Reserved.     *
 ************************************************************************
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.        *
 *                                                                      *
 * This code is free software; you can redistribute it and/or modify it *
 * under the terms of The MIT License (MIT), as published by the Open   *
 * Source Initiative. (See http://opensource.org/licenses/MIT)          *
 ************************************************************************/

public class Main {
    /**// www.  j av  a2  s.c  o  m
     * This function converts the bytes in a byte array to its corresponding double value.
     *
     * @param buffer The byte array containing the double.
     * @return The corresponding double value.
     */
    static public double bytesToDouble(byte[] buffer) {
        return bytesToDouble(buffer, 0);
    }

    /**
     * This function converts the bytes in a byte array at the specified index to its
     * corresponding double value.
     *
     * @param buffer The byte array containing the double.
     * @param index The index for the first byte in the byte array.
     * @return The corresponding double value.
     */
    static public double bytesToDouble(byte[] buffer, int index) {
        double real;
        long bits = bytesToLong(buffer, index);
        real = Double.longBitsToDouble(bits);
        return real;
    }

    /**
     * This function converts the bytes in a byte array to its corresponding long value.
     *
     * @param buffer The byte array containing the long.
     * @return The corresponding long value.
     */
    static public long bytesToLong(byte[] buffer) {
        return bytesToLong(buffer, 0);
    }

    /**
     * This function converts the bytes in a byte array at the specified index to its
     * corresponding long value.
     *
     * @param buffer The byte array containing the long.
     * @param index The index for the first byte in the byte array.
     * @return The corresponding long value.
     */
    static public long bytesToLong(byte[] buffer, int index) {
        int length = buffer.length - index;
        if (length > 8)
            length = 8;
        long l = 0;
        for (int i = 0; i < length; i++) {
            l |= ((buffer[index + length - i - 1] & 0xFFL) << (i * 8));
        }
        return l;
    }
}

Related

  1. byteArrayToDouble(byte[] bytes)
  2. byteArrayToDoubleArray(final byte[] raw, final boolean bigEndian, final int length)
  3. byteArrayToDoubleBE(byte[] data)
  4. bytes2double(byte[] b)
  5. bytes2double(byte[] bytes)
  6. bytesToDouble(byte[] bytes)
  7. bytesToDouble(byte[] bytes)
  8. bytesToDouble(byte[] bytes, int off)
  9. bytesToDouble(byte[] bytes, int offset)