0

I am receiving 8 bytes from serial.

I want to convert and save in double variable.

This is how i tried.

union u_data
    {
        byte b[4];
        double fval;
    } u;

double BytesToDouble (byte b8,byte b7,byte b6,byte b5,byte b4,byte b3,byte b2,byte b1)
{   
    u.b[0] = b1;
    u.b[1] = b2;
    u.b[2] = b3;
    u.b[3] = b4;
    u.b[4] = b5;
    u.b[5] = b6;
    u.b[6] = b7;
    u.b[7] = b8;
 return u.fval;
}

But i am getting 0.00. Because here double size is 4 bytes.

Is there is any other way to do this?

Thanks

1
  • 1
    How is u.b[7] = b8; supposed to work on an array that is of size 4? How are you calling BytesToDouble?
    – Gerben
    Dec 18, 2015 at 16:46

2 Answers 2

2

In the Arduino there is no such thing as a double. A double is just another name for a float.

So you will have to either:

  1. Manually interpret the double format data and calculate that into a float value, or
  2. Send the value as some other format, such as float (32-bit), ASCII text, or (a selection of) integer values.

Since you can't actually use anything more than float, sending data with a higher resolution is pointless, so you are probably best off just sending it as a float.

1

avr-gcc doesn't support binary64. You can use 8 bytes to treat it as an opaque data type, but attempting to manipulate it beyond manually converting to/from binary32 will be naught but pain.

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.