Float-type data transfer between different systems

Source: Internet
Author: User
Tags float double

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.