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

Java examples for java.lang:byte array to long

Description

Write a long 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;
        long v = 2;
        writeLong(array, offset, v);/*from w  w  w. j av a 2 s.co  m*/
    }

    /**
     * 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