Write a double to the byte array at the given offset. - Java java.lang

Java examples for java.lang:byte Array to double

Description

Write a double to the byte array at the given offset.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte[] array = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        int offset = 2;
        double v = 2.45678;
        writeDouble(array, offset, v);/*  w  w  w  . j  a v  a  2 s  .c  o m*/
    }

    /**
     * Write a double to the byte array at the given offset.
     * 
     * @param array Array to write to
     * @param offset Offset to write to
     * @param v data
     */
    public final static void writeDouble(byte[] array, int offset, double v) {
        writeLong(array, offset, Double.doubleToLongBits(v));
    }

    /**
     * Write a long to the byte array at the given offset.
     * 
     * @param array Array to write to
     * @param offset Offset to write to
     * @param v data
     */
    public final static void writeLong(byte[] array, int offset, long v) {
        array[offset + 0] = (byte) (v >>> 56);
        array[offset + 1] = (byte) (v >>> 48);
        array[offset + 2] = (byte) (v >>> 40);
        array[offset + 3] = (byte) (v >>> 32);
        array[offset + 4] = (byte) (v >>> 24);
        array[offset + 5] = (byte) (v >>> 16);
        array[offset + 6] = (byte) (v >>> 8);
        array[offset + 7] = (byte) (v >>> 0);
    }
}

Related Tutorials