#include <stdio.h> #include <string.h>
int main (void) {
Union result
{float D; unsigned char data[4]; }R1,R2;
In particular, it is important to note that the word length in the single-chip int float double type is not the same as in the PC.
r1.d=7.215487 23131;
R2.DATA[0]=R1.DATA[0];
R2.DATA[1]=R1.DATA[1];
R2.DATA[2]=R1.DATA[2];
R2.DATA[3]=R1.DATA[3];
printf ("byte0=%d byte1=%d byte2=%d byte3=%d r1=%f r2=%f \ n", r1.data[0],r1.data[1],r1.data[2],r1.data[3],r1.d,r2.d);
GetChar ();
GetChar ();
return 0; }
Using the above method, if necessary between the PC and MCU through the serial port to transmit the floating-point number double/float can be, both sides use a common body, a byte at a time, and then put into the char array, the use of the corresponding floating-point number can be used directly. 2 Memory operation function/Call library function
You can use library functions directly, since the Commons is a shared address space between different data types, we can also use the memory copy to complete the functionality that the common body is going to accomplish. The code is as follows:
#include <stdio.h>
#include <string.h>
int main (void) {
float pi=7.123145,test;
Char byte[4];
memcpy (byte,&pi,sizeof (float));
printf ("%d%d%d%d \ n", byte[0],byte[1],byte[2],byte[3]);
memcpy (&test,byte,sizeof (float));
printf ("Result%f \ n", test);
return 0; }
Float-type data transfer between different systems