1. Overview

In this article we will discuss various techniques of converting long to byte array and vice versa,long array to byte array and so on.In java long data type take 8 bytes and it’s range is -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

2. long to byte array

Here we have discussed three different ways to convert long to byte array in java as below:

2.1 DataOutputStream

private byte[] longToByteArray ( final long i ) throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bos);
        dos.writeLong(i);
        dos.flush();
        return bos.toByteArray();
}

2.2 ByteBuffer

private long convertByteArrayToLong(byte[] longBytes){
    ByteBuffer byteBuffer = ByteBuffer.wrap(longBytes);
    byteBuffer.flip(); 
    return byteBuffer.getLong();
}

2.3 Shift operations

private static byte[] longtoBytes(long data) {
 return new byte[]{
 (byte) ((data >> 56) & 0xff),
 (byte) ((data >> 48) & 0xff),
 (byte) ((data >> 40) & 0xff),
 (byte) ((data >> 32) & 0xff),
 (byte) ((data >> 24) & 0xff),
 (byte) ((data >> 16) & 0xff),
 (byte) ((data >> 8) & 0xff),
 (byte) ((data >> 0) & 0xff),
 };
}

3. byte array to long

We can convert any byte array to long using below two techniques.

3.1 ByteBuffer

Java provide ByteBuffer class to do the same.to convert any byte array, first we need to allocate 8 bytes using ByteBuffer’s static method allocate, then put byteArray using put method and flip bytebuffer. by calling getLong() method we can get long value of that byte array.

private long convertByteArrayToLong(byte[] longBytes){
 ByteBuffer byteBuffer = ByteBuffer.allocate(Long.BYTES);
 byteBuffer.put(longBytes);
 byteBuffer.flip();
 return byteBuffer.getLong();
}

3.2 Shift operations

Second way of convery byte array to long is by using shift operator and & operator.

public long convertByteArrayToLongArrayByShift(byte[] data) {
 if (data == null || data.length % Long.BYTES != 0) return -1;
 // ----------
 return ( convertByteArrayToLong(new byte[] {
 data[(Long.BYTES)],
 data[(Long.BYTES)+1],
 data[(Long.BYTES)+2],
 data[(Long.BYTES)+3],
 data[(Long.BYTES)+4],
 data[(Long.BYTES)+5],
 data[(Long.BYTES)+6],
 data[(Long.BYTES)+7],
 } ));
}

4. long array to byte array

private byte[] convertLongArrayToByteArray(long[] data) {
 if (data == null) return null;
 // ----------
 byte[] byts = new byte[data.length * Long.BYTES];
 for (int i = 0; i < data.length; i++)
 System.arraycopy(convertLongToByteArray(data[i]), 0, byts, i * Long.BYTES, Long.BYTES);
 return byts;
}

5. byte array to long array

public long[] convertByteArrayToLongArray(byte[] data) {
 if (data == null || data.length % Long.BYTES != 0) return null;
 // ----------
 long[] longs = new long[data.length / Long.BYTES];
 for (int i = 0; i < longs.length; i++)
 longs[i] = ( convertByteArrayToLong(new byte[] {
 data[(i*Long.BYTES)],
 data[(i*Long.BYTES)+1],
 data[(i*Long.BYTES)+2],
 data[(i*Long.BYTES)+3],
 data[(i*Long.BYTES)+4],
 data[(i*Long.BYTES)+5],
 data[(i*Long.BYTES)+6],
 data[(i*Long.BYTES)+7],
 } ));
 return longs;
}

5. Conclusion

In this article we have discussed various techniques of converting byte array to long, long to byte array, long array to byte array and byte array to long array using ByteBuffer,DataInputStream,DataOutputStream and some custom logic.

6. Reference

Refer ByteBuffer , DataInputStream for more details.

7. Source Code

You can also download a complete example from our git repository.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *