0

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);
3
  • 4
    Shifting a double? You don't really want to do that. Commented Feb 5, 2017 at 18:24
  • Please show the declarations of myArray and myDouble to show their exact type.
    – jfpoilpret
    Commented Feb 5, 2017 at 22:31
  • I edited the question. Commented Feb 6, 2017 at 8:08

2 Answers 2

2

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.

Example:

  union 
    {
    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).

0

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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