If I store in an array a double-value in this format, in which format I should be able to read the same double out?

double myDouble = 12.123456;
byte myArray[] = {0x00, 0x00, 0x00, 0x00};

myArray[0] = (myDouble >> 0);
myArray[1] = (myDouble >> 8);
myArray[2] = (myDouble >> 16);
myArray[3] = (myDouble >> 24);
  • 4
    Shifting a double? You don't really want to do that. – Edgar Bonet Feb 5 '17 at 18:24
  • Please show the declarations of myArray and myDouble to show their exact type. – jfpoilpret Feb 5 '17 at 22:31
  • I edited the question. – William Roy Feb 6 '17 at 8:08

You are better off making a union, that will be compiler independent, and won't give you the (possibly) odd results that trying to shift a double would give.


    byte myArray [4];
    double myDouble;
    } foo;

   foo.myArray [0] = Serial.read ();
   foo.myArray [1] = Serial.read ();
   foo.myArray [2] = Serial.read ();
   foo.myArray [3] = Serial.read ();

   double f = foo.myDouble;

Even then be cautious that whatever is sending you this double in 4 bytes is using the same encoding for doubles that the Arduino does. For one thing, double and float are the same on the 8-bit Arduinos (they are only 4 bytes).


in the simplest form (compiler dependent however), a pointer will do.

  • 2
    Would you mind developing your answer further, so that we can clearly see how it really answers the OP question? – jfpoilpret Feb 5 '17 at 22:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.