This is the so-called ieee754 standard, which is also the Standard for most hardware-stored floating point numbers. A single-precision floating point occupies 4 bytes, indicating that the range is: When the negative number is
-3.402823e38 to-1.401298e-45, while in positive numbers, it is from 1.401298e-45 to 3.402823e38.
The Conversion Function in C # is:
1. It is converted from a four-byte hexadecimal array to a floating point number:
Byte [] bytes = new byte [4];
Bitconverter. tosingle (bytes, 0 );
2. Convert from floating point to array:
Byte [] bytes = bitconverter. getbytes (floatvalue );
This conversion method is often used in serial communication, indicating that the range is sufficient for the numerical transmission of various sensors and industrial control scenarios. The floating point data to be sent is converted to the hexadecimal number of four bytes, which is then sent by the serial port, convert it to a floating point number at the receiving end.
Single-Chip Microcomputer or non-. NET environment ConversionProgramThe bitconverter class cannot be called!
Provide the followingCodeFor conversion:
The unmodified ones are as follows: You can directly call them in C # without using database functions.
Public static float tofloat (byte [] data)
{
Float a = 0;
Byte I;
Byte [] x =
Data;
Unsafe
{
Void *
PF;
Fixed (byte * PX = X)
{
PF = &;
For (I = 0; I <
Data. length; I ++)
{
* (Byte *) PF + I) = * (PX + I );
}
}
}
Return;
}
Public static byte [] tobyte (float data)
{
Unsafe
{
Byte * pdata =
(Byte *) & data;
Byte [] bytearray = new
Byte [sizeof (float)];
For (INT I = 0; I <sizeof (float );
++ I)
Bytearray [I] =
* Pdata ++;
Return bytearray;
}
}
If you compile the project directly, an error is returned: C # does not support pointers by default. pointers can be used only in the form of Insecure code.
Error 1 Unsafe code only appears when/unsafe compilation is used
2008 \ projects \ test \ testoffloatconsolt \ Program. CS 26 13 testoffloatconsolt select project> "project" Property> Generate> General> Insecure code in the menu bar of
Select
MCU serial port communication floating point Conversion Function
I used this part in the AVR serial communication protocol. I directly converted the single-chip computer's computation result (floating point type) into (byte type) and embedded it into the serial communication protocol, and uploaded it to the upper computer.
The following is a function that converts a floating point number to an array of four bytes in line with the ieee754 standard.Source code: It is already used in serial communication of the single-chip microcomputer.
WinAVR-20090313 test passed:
Void floattobyte (float floatnum, unsigned char * bytearry)
{
Char *
Pchar = (char *) & floatnum;
For (int
I = 0; I <sizeof (float); I ++)
{
* Bytearry = * pchar;
Pchar ++;
Bytearry ++;
}
}
The following shows how to convert a four-byte array to a floating point number that complies with the ieee754 standard.
WinAVR-20090313 test passed:
Float bytetofloat (unsigned char * bytearry)
{
Return
* (Float *) bytearry );
}
Call the Test method: usart_transmit (); indicates the function sent to the serial port.
Unsigned char
Floattobyte [4];
Floattobyte (12.15, floattobyte );
Float
A = bytetofloat (floattobyte );
Floattobyte (A, floattobyte );
Usart_transmit (floattobyte [0]);
Usart_transmit (floattobyte [1]);
Usart_transmit (floattobyte [2]);
Usart_transmit (floattobyte [3]);
Called when the host machine reads data through a serial port
Bitconverter. tosingle (bytes, 0 );
It is converted to 12.15, and the test method can change as needed.
Transferred from:
Http://hi.baidu.com/wgnzebpqjsbntxd/item/5a7a3eda302f0e3f2a35c77d