1

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.

mch
  • 9,424
  • 2
  • 28
  • 42
  • Your code seems syntactically correct (can't check rn though), if you want a review you should rather go to [Code Review SE](https://codereview.stackexchange.com/) :) – joH1 Jul 23 '18 at 06:37
  • Anyway why can't you just do the reverse process? copy your bytes in the union and return its `double` member – joH1 Jul 23 '18 at 06:44
  • Does your code work as intended or not? – Jabberwocky Jul 23 '18 at 06:44
  • Just as a side note: You should name your function `double2Bytes` and not `float2Bytes` – BluesSolo Jul 23 '18 at 06:47
  • First step is working but I don't know whether is working correct or not. I have to make second part of the code to be sure first step is correct or not. – Yavuzhan Erdem Jul 23 '18 at 07:47

2 Answers2

3

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.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • @EricPostpischil: My English is not good enough to differentiate between unnecessary and useless (I would have say *inutile* in French). Edited including the interchange question. – Serge Ballesta Jul 23 '18 at 12:07
  • Thank you for help.Your code which is for second part of my code is work . – Yavuzhan Erdem Jul 23 '18 at 14:05
0

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;
}
Programmer dude
  • 167
  • 1
  • 5
  • 23
  • 1
    It should be noted that `double c = *((double *)pmessage);` is valid only because `pmessage` was assigned a value converted from a pointer to `double`. If it pointed to an object defined as an array of `uint8_t`, this would not be proper code. – Eric Postpischil Jul 23 '18 at 11:52
  • Any answer should caution OP that there are endianness and format issues. The question mentions that the OP wants to “send” the data. This suggests they may be transmitting the data to another system. Even if the other system uses the same format for `double` (likely IEEE-754 basic 64-bit binary floating-point), it may have the bytes in another order. Additionally, systems can use different formats for `double`. – Eric Postpischil Jul 23 '18 at 11:54
  • Data which is converted from double to uint8_t is used to send to matlab simulink.After i processed the data in matlab again i send to back to conver uint8_t to double .This is my system. – Yavuzhan Erdem Jul 23 '18 at 12:13
  • @YavuzhanErdem I've sent data between a C-program and matlab passing strings through named pipes. It seemed simpler than the proprietary binary protocol, and part from the pipe setup (I messed up pretty bad) I was quite pleased with the result. – Andreas Jul 23 '18 at 12:39
  • My codes worked very well according to my expectation. I sent my data from C-program to Matlab via UDP so again I returned back over udp. – Yavuzhan Erdem Jul 23 '18 at 15:01