Convert byte array to double value. - Java java.lang

Java examples for java.lang:byte Array Convert

Description

Convert byte array to double value.

Demo Code


//package com.java2s;

import javax.annotation.Nullable;
import java.nio.ByteBuffer;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte[] value = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(bytesToDouble(value));
    }//  www.j  a  va2  s  .  c om

    /**
     * Convert byte array to double value.
     */
    public static double bytesToDouble(@Nullable byte[] value) {
        if (value == null)
            return Double.NaN;
        if (value.length != Double.BYTES)
            throw new IllegalArgumentException("value length is not double");

        return ByteBuffer.allocate(Double.BYTES).put(value).getDouble(0);
    }
}

Related Tutorials