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

Byte arrays are commonly used in applications that stream data byte-wise, such as socket connections that send data in byte arrays through TCP or UDP protocols.
It also used in applications that read/write binary files. Here we have discussed three different ways to convert double to byte array in java as below:

2.1 ByteArrayOutputStream

First way to convert any double value to byte array is using ByteArrayOutputStream and DataOutputStream.  ByteArrayOutputStream class provide one method toByteArray which return underlying written primitive types into a byte array format. Here we have write double to underlying stream and then use the toByteArray method to get equivalent byte array.

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

java.nio provide a facility which help us to work with buffers very efficiently. There are several buffer classes like ByteBuffer, IntBuffer, LongBuffer,DoubleBuffer MappedByteBuffer etc. under java.nio package. Here we have used ByteBuffer class to convert double value to byte array using it’s array method.

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

2.3 Shift operations

Shift Operation >> along with bitwise & operator used to convert double datatype valueto a byte array.  Before that, we need to convert a double value to long type and then use shift operation to make it work.

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 provides 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

The second way of the 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

Now double array to byte array is easy as we have already discussed for a single double value. We will iterate all the elements from the double array and convert it to byte array and append all elements bytes in a single large 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

Similar way we will take chunks of 8 bytes and convert those chunks into double and store it into array for later use.

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

7. Source Code

You can download the source code of from our git repository

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 *