1. Overview

In this article we will discuss various techniques of converting double to byte array and vice versa,double array to byte array and so on.In java double data type take 8 bytes and it’s range is ±1.79769313486231570E+308.

2. double to byte array

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

2.1 ByteArrayOutputStream

private byte[] doubleToByteArray ( final double i ) throws IOException {
 ByteArrayOutputStream bos = new ByteArrayOutputStream();
 DataOutputStream dos = new DataOutputStream(bos);
 dos.writeDouble(i);
 dos.flush();
 return bos.toByteArray();
}

2.2 ByteBuffer

private byte [] convertDoubleToByteArray(double number) {
 ByteBuffer byteBuffer = ByteBuffer.allocate(Double.BYTES);
 byteBuffer.putDouble(number);
 return byteBuffer.array();
}

2.3 Shift operations

private static byte[] doubletoBytes(double dblValue) {
 long data = Double.doubleToRawLongBits(dblValue);
 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 double

We can convert any byte array to double 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 getDouble() method we can get double value of that byte array.

private double convertByteArrayToDouble(byte[] doubleBytes){
 ByteBuffer byteBuffer = ByteBuffer.allocate(Double.BYTES);
 byteBuffer.put(doubleBytes);
 byteBuffer.flip();
 return byteBuffer.getDouble();
}

3.2 Shift operations

Second way of convert byte array to double is by using shift operator and & operator.

public double convertByteArrayToDoubleShiftOpr(byte[] data) {
 if (data == null || data.length % Double.BYTES != 0) return Double.NaN;
 // ----------
 
 return ( convertByteArrayToDouble(new byte[]{
 data[(Double.BYTES)],
 data[(Double.BYTES) + 1],
 data[(Double.BYTES) + 2],
 data[(Double.BYTES) + 3],
 data[(Double.BYTES) + 4],
 data[(Double.BYTES) + 5],
 data[(Double.BYTES) + 6],
 data[(Double.BYTES) + 7],
 }
 ));
}

4. double array to byte array

private byte[] convertDoubleArrayToByteArray(double[] data) {
 if (data == null) return null;
 // ----------
 byte[] byts = new byte[data.length * Double.BYTES];
 for (int i = 0; i < data.length; i++)
 System.arraycopy(convertDoubleToByteArray(data[i]), 0, byts, i * Double.BYTES, Double.BYTES);
 return byts;
}

5. byte array to double array

public double[] convertByteArrayToDoubleArray(byte[] data) {
 if (data == null || data.length % Double.BYTES != 0) return null;
 // ----------
 double[] doubles = new double[data.length / Double.BYTES];
 for (int i = 0; i < doubles.length; i++)
 doubles[i] = ( convertByteArrayToDouble(new byte[] {
 data[(i*Double.BYTES)],
 data[(i*Double.BYTES)+1],
 data[(i*Double.BYTES)+2],
 data[(i*Double.BYTES)+3],
 data[(i*Double.BYTES)+4],
 data[(i*Double.BYTES)+5],
 data[(i*Double.BYTES)+6],
 data[(i*Double.BYTES)+7],
 } ));
 return doubles;
}

5. Conclusion

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

6. Reference

Refer ByteBuffer , DataInputStream , ByteArrayOutputStream for more details.

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No

Leave a Reply

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