# Convert double to bytes

**URL:** <https://discourse.processing.org/t/convert-double-to-bytes/12154>\
**Category:** Coding Questions\
**Created:** [June 18, 2019, 2:17pm UTC](https://discourse.processing.org/t/convert-double-to-bytes/12154 "2019-06-18T14:17:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![x13420x](https://avatars.discourse-cdn.com/v4/letter/x/ecc23a/32.png) [@x13420x](https://discourse.processing.org/u/x13420x)\
**Post date:** [June 18, 2019, 2:17pm UTC](https://discourse.processing.org/t/convert-double-to-bytes/12154/1 "2019-06-18T14:17:32Z")

</div>

I need to convert a single array of doubles to 8 arrays of bytes to save…but not sure this is possible…currently I am converting to float then to str…to save as txt…but there is some loss with this…I would prefer to preserve the double precision also the savestrings function seems really sluggish and inefficient.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [June 19, 2019, 8:56am UTC](https://discourse.processing.org/t/convert-double-to-bytes/12154/2 "2019-06-19T08:56:01Z")

</div>

i not think you need a text file???

can use the library [BezierSQLib](https://github.com/fjenett/sql-library-processing)  
to create a SQLite3 database file  
and try save values  
[https://sqlite.org/datatype3.html](https://sqlite.org/datatype3.html)

> **REAL**. The value is a floating point value, stored as an 8-byte IEEE floating point number.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [June 19, 2019, 9:56am UTC](https://discourse.processing.org/t/convert-double-to-bytes/12154/3 "2019-06-19T09:56:34Z")

</div>

The following sketch will convert double\>byte and byte\>double arrays.  
Sample output:

```auto
Original array
0.24904495375353408 0.15873060773194125 0.24651129906363822 0.2543479093984985
Array restored from byte array
0.24904495375353408 0.15873060773194125 0.24651129906363822 0.2543479093984985

```

.

```auto
import java.nio.ByteBuffer;
import java.util.Random;

void setup() {
  Random rnd = new Random();
  double[] d1 = new double[4];
  for (int i = 0; i < d1.length; i++) {
    d1[i] = rnd.nextDouble();
  }
  println("Original array");
  showArray(d1);
  // Get byte array from double array
  byte[] b = convertDoubleToByteArray(d1);
  // Restore double array
  double[] d2 = convertByteToDoubleArray(b);
  println("Array restored from byte array");
  showArray(d2);
}

void showArray(double[] doubles) {
  for (double d : doubles) {
    print(" " + d);
  }
  println();
}

byte[] convertDoubleToByteArray(double[] doubles) {
  ByteBuffer bb = ByteBuffer.allocate(doubles.length * 8);
  for (double d : doubles) {
    bb.putDouble(d);
  }
  return bb.array();
}

double[] convertByteToDoubleArray(byte[] bytes) {
  ByteBuffer bb = ByteBuffer.wrap(bytes);
  double[] doubles = new double[bytes.length / 8];
  for (int i = 0; i < doubles.length; i++) {
    doubles[i] = bb.getDouble();
  }
  return doubles;
}

```

---

<div class="post-metadata">

**Author:** ![x13420x](https://avatars.discourse-cdn.com/v4/letter/x/ecc23a/32.png) [@x13420x](https://discourse.processing.org/u/x13420x)\
**Post date:** [June 19, 2019, 1:28pm UTC](https://discourse.processing.org/t/convert-double-to-bytes/12154/4 "2019-06-19T13:28:23Z")

</div>

👍👍👍👍👍👍👍👍👍👍👍👍👍👍

Thank you for the responses…I will go with quarks suggestion…

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 22, 2019, 11:36pm UTC](https://discourse.processing.org/t/convert-double-to-bytes/12154/5 "2019-06-22T23:36:38Z")

</div>

I managed to do this with lots of bit shifting, masking, promoting… what a workout that was!  
I like to write code as an exercise without too much influence and got it working!  
Something like this:  
double \> Double.doubleToLongBits \> byte array \> Double.longBitsToDouble \> double  
I did a lot of this in my early years coding in assembly and C.

I am going to try this with my Arduino to Processing.  
I can take those doubles and pack them into bytes and do eight serial writes!

My Arduino sends the following quaternion data as viewed on serial monitor:

> 0.99395751953125,-0.10644531250000,0.02075195312500,0.01812744140625  
> 0.99743652343750,-0.05615234375000,0.04071044921875,0.01690673828125

Arduino Code:

```auto
void plotQuat0()
  { 
// Quaternion data
  imu::Quaternion quat = bno.getQuat();
  
    Serial.print(quat.w(), 14);
    Serial.print(',');  
    Serial.print(quat.x(), 14);
    Serial.print(',');
    Serial.print(quat.y(), 14);
    Serial.print(',');
    Serial.print(quat.z(), 14);
    Serial.print('\n');
  }

```

Thanks for inspiring me to do this!

🙂
