Yavuzhan Erdem
Yavuzhan Erdem

Reputation: 23

how to convert double data to uint8 array and again return to double

I want to get data as a double so after that i send the data as uint8_t array. So I determined 2 steps.Steps;

1-First Step :Double to uint8_t

#include <stdint.h>
#include <stdio.h>
#include <string.h>
void float2Bytes(double val,uint8_t* bytes_array);

int main(void) {
    double b=1690.000000;
    uint8_t message[1024];
    float2Bytes(b,&message[0]);
    int ii;
    for (ii=0; ii<8; ii++) 
        printf ("byteS %d is %02x\n", ii, message[ii]);
    return 0;
}

void float2Bytes(double val,uint8_t* bytes_array){
    // Create union of shared memory space
    union {
        double double_variable;
        uint8_t temp_array[8];
    } u;
    // Overite bytes of union with float variable
    u.double_variable = val;
    // Assign bytes to input array
    memcpy(bytes_array, u.temp_array, 8);
}

2-Second Step : uint8_t array to Double

Can you advise at the this stages ? How can I do ? And can you examine at the first stage whether there are errors or not. ?

Thank you.

Upvotes: 1

Views: 6550

Answers (2)

Serge Ballesta
Serge Ballesta

Reputation: 149125

The union is unnecessary here. Character pointers are special in C and can be used to extract the byte representation of any type. That mean that your current main could be stripped down to:

int main(void) {
    double b=1690.000000;
    uint8_t* pmessage = (char *) &b;    // legal and portable C
    int ii;
    for (ii=0; ii<sizeof(double); ii++) // portable C
        printf ("byteS %d is %02x\n", ii, message[ii]);
    return 0;
}

Fro the second step, you need a memcpy operation to copy from a byte array to a different type. Here again no union required for the exact same reason: a char pointer can be used to write the byte representation of any type:

double doubleFromBytes(uint8_t *buffer) {
    double result;
    // legal and portable C provided buffer contains a valid double representation
    memcpy(&result, buffer, sizeof(double))
    return result;
}

The only assumption here is that buffer points to an byte array containing the byte representation of a double.


Of course, what you get here is the representation of a double for current architecture. The representation of the same double value can be different on a different architecture (*). It might be a problem if to intend to send the representation to a different machine or to a program compiled with different options.

(*): at least endianness (order of bytes) can be different. Not speaking of exotic (no IEC 60559 floating point) representations where sizeof(double) could be different of 8.

Upvotes: 3

Programmer dude
Programmer dude

Reputation: 167

You only need a pointer to do this.

int main(void)
{
    double b=1690.000000;
    // double to uint8 array
    uint8_t* pmessage = (uint8_t*) &b;
    int ii;
    for (ii=0; ii<sizeof(double); ii++)
        printf ("byteS %d is %02x\n", ii, message[ii]);

    // uint8 array to ddouble
    double c = *((double *)pmessage);
    printf ("double %f\n", c);

    return 0;
}

Upvotes: 0

Related Questions