java convert double to byte array

Java Catechumen double to byte array

Table of Contents

  • 1. Overview
  • two. double to byte array
    • ii.1 ByteArrayOutputStream
    • 2.2 ByteBuffer
    • 2.3 Shift operations
  • 3. byte assortment to double
    • three.i ByteBuffer
    • iii.2 Shift operations
  • 4. double array to byte array
  • five. byte array to double array
  • v. Conclusion
  • 6. Reference
  • 7. Source Code
    • Was this post helpful?

1. Overview

In this article we volition discuss various techniques of converting double to byte array and vice versa,double array to byte array then on.In java double data type have viii bytes and it'due south range is ±ane.79769313486231570E+308.

2. double to byte array

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

two.ane ByteArrayOutputStream

First way to convert whatsoever double value to byte array is usingByteArrayOutputStream andDataOutputStream.  ByteArrayOutputStream grade provide ane methodtoByteArray which return underlying written archaic types into a byte array format. Here we take write double to underlying stream and and so use thetoByteArray method to get equivalent byte array.

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

2.2 ByteBuffer

coffee.nio provide a facility which assistance u.s.a. to work with buffers very efficiently. In that location are several buffer classes like ByteBuffer, IntBuffer, LongBuffer,DoubleBuffer MappedByteBuffer etc. undercoffee.nio package. Here nosotros take used ByteBuffer class to convert double value to byte array using it'sassortment method.

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

2.3 Shift operations

Shift Operation >> forth with bitwise & operator used to convert double datatype valueto a byte array.  Before that, we need to catechumen a double value to long blazon and then use shift operation to brand information technology piece of work.

private static byte[] doubletoBytes(double dblValue) {  long data = Double.doubleToRawLongBits(dblValue);  return new byte[]{  (byte) ((data >> 56) & 0xff),  (byte) ((information >> 48) & 0xff),  (byte) ((data >> forty) & 0xff),  (byte) ((data >> 32) & 0xff),  (byte) ((data >> 24) & 0xff),  (byte) ((data >> xvi) & 0xff),  (byte) ((information >> viii) & 0xff),  (byte) ((data >> 0) & 0xff),  }; }

three. byte assortment to double

We can convert any byte array to double using beneath two techniques.

3.i ByteBuffer

Coffee provides ByteBuffer course to do the same.to convert any byte array, first we need to allocate 8 bytes using ByteBuffer'southward static method allocate, then put byteArray using put method and flip bytebuffer. by calling getDouble() method we can get double value of that byte assortment.

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

3.2 Shift operations

The 2nd way of the convert byte array to double is by using shift operator and & operator.

public double convertByteArrayToDoubleShiftOpr(byte[] data) {  if (information == zip || data.length % Double.BYTES != 0) render Double.NaN;  // ----------    render ( convertByteArrayToDouble(new byte[]{  data[(Double.BYTES)],  data[(Double.BYTES) + 1],  data[(Double.BYTES) + 2],  data[(Double.BYTES) + 3],  information[(Double.BYTES) + iv],  data[(Double.BYTES) + v],  data[(Double.BYTES) + 6],  data[(Double.BYTES) + 7],  }  )); }

four. double array to byte array

Now double array to byte assortment is piece of cake as we accept already discussed for a single double value. Nosotros will iterate all the elements from the double assortment and convert it to byte array and suspend all elements bytes in a single large byte array.

individual byte[] convertDoubleArrayToByteArray(double[] information) {  if (data == null) render 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);  render byts; }

5. byte array to double array

Like mode nosotros will take chunks of 8 bytes and convert those chunks into double and store information technology into array for later use.

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

5. Determination

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 assortment to double assortment using ByteBuffer,ByteArrayOutputStream,DataInputStream,DataOutputStream and some custom logic.

6. Reference

  • ByteBuffer
  • DataInputStream
  • ByteArrayOutputStream
  • Java Basic Blogs
  • Coffee 8 Blogs

seven. Source Code

You can download the source code of from our git repository

Was this post helpful?