If it is large-end storage mode, after executing the above statement,
the data stored in outdata of address unit is 0x41
O 1 stores data as 0x8D
address unit (o 2) stores data as 0x00
address unit (o 3) stores data as 0x00
2. If the compiler used by the user does not implement the library function of this function, the
following functions can be used to achieve this function:
void memcpy(void *dest,void *src,int n)
{
char *pd = (char *)dest; char *ps = (char *)src;
for(int i=0;i<n;i++) *pd++ = *ps++;
}
And then make a call to the above memcpy(outdata,&floatdata,4);
Example: Compile binary floating-point number 0100 0010 0111 1011 0110 0110 0110 10B to
decimal number
Step 1: Divide the binary floating-point number 0100 0010 0111 1011 0110 0110 0110B into symbol
bit, exponential bit and mantissa bit.
0 10000100 11110110110011001100110B
1-bit sign + 8-bit index + 23-bit tail sign bit S: 0 denotes positive number
Index position E
:
10000100B =
1×2
7
+0×2
6
+0×2
5
+0×2
4
+ 0 × 2
3
+1×2
2
+0×2
1
+0×2
0
=128+0+0+0+0+4+0+0=132
Mantissa bits M
:
11110110110011001100110B =8087142
Step 2: Calculate the decimal number
D = (
−
1)
𝑆
×(1.0 + M/2
23
)×2
𝐸
−
127
= (
−
1)
0
×(1.0 + 8087142/2
23
)×2
132
−
127
= 1×1.964062452316284×32
= 62.85
Reference Code:
float floatTOdecimal(long int byte0, long int byte1, long int byte2, long int byte3)
{ long int realbyte0,realbyte1,realbyte2,realbyte3; char S;
long int E,M;
float D;
realbyte0 = byte3; realbyte1 = byte2; realbyte2 = byte1; realbyte3 = byte0;
if((realbyte0&0x80)==0)
{ S = 0;//positive number }
else { S = 1;//negative number }
E = ((realbyte0<<1)|(realbyte1&0x80)>>7)-127;
M = ((realbyte1&0x7f) << 16) | (realbyte2<< 8)| realbyte3;
D = pow(-1,S)*(1.0 + M/pow(2,23))* pow(2,E);
return D; }
Function description: parameters byte0, byte1, byte2, byte3 represent 4 bytes of binary floating point
number
(
The decimal number converted from the return value
For example, the user sends the command to get the temperature value and dissolved oxygen value
to the probe. The4 bytes representing the temperature value in the received response frame are 0x00,
0x00, 0x8d and 0x41. Then the user can get the decimal number of the corresponding temperature
value through the following call statement.
That is temperature = 17.625.
float temperature = floatTOdecimal( 0x00, 0x00, 0x8d, 0x41)
17