Converts a byte array to a long array. - Java java.lang

Java examples for java.lang:byte Array Convert

Description

Converts a byte array to a long array.

Demo Code


//package com.java2s;
import java.nio.ByteBuffer;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte[] bytes = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(java.util.Arrays
                .toString(convertByteArrayToLongArray(bytes)));
    }//from   w ww .  java 2  s  .  c  om

    /**
     * Converts a byte array to a long array.
     * @param bytes
     * @return
     */
    public static long[] convertByteArrayToLongArray(byte[] bytes) {
        if (bytes == null)
            return null;

        int count = bytes.length / 8;
        long[] longArray = new long[count];
        ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
        for (int i = 0; i < count; i++) {
            longArray[i] = byteBuffer.getLong();
        }
        return longArray;
    }
}

Related Tutorials