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.

5
  • Your code seems syntactically correct (can't check rn though), if you want a review you should rather go to Code Review SE :) Commented Jul 23, 2018 at 6:37
  • Anyway why can't you just do the reverse process? copy your bytes in the union and return its double member Commented Jul 23, 2018 at 6:44
  • Does your code work as intended or not? Commented Jul 23, 2018 at 6:44
  • Just as a side note: You should name your function double2Bytes and not float2Bytes Commented Jul 23, 2018 at 6: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. Commented Jul 23, 2018 at 7:47

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@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.
Thank you for help.Your code which is for second part of my code is work .
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; } 

5 Comments

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.
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.
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.
@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.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.