C400 Series Operating Manual
25
Reference code:
1. If the compiler used by the user has a library function that implements this function, the library function can be called directly, for
example, using C language, then you can directly call the C library function memcpy to obtain an integer representation of the floating-
point storage format in memory. For example: float floatdata; // converted floating point number
void* outdata;
memcpy (outdata,&floatdata,4);
Suppose floatdata = 17.625
If it is a small-end storage mode, after executing the above statement,
the data stored in the address unit outdata is 0x00.
address unit (o 1) stores data as 0x00
address unit (o 2) stores data as 0x8D
address unit (o 3) stores data as 0x41
If it is large-end storage mode, after executing the above statement,
the data stored in outdata of address unit is 0x41
address unit (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 0110B to decimal number.
Step 1:
Divide the binary float-
ing-point number 0100 0010 0111 1011 0110 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 = 1x27 + 0x26 + 0x25 + 0x25 + 0x24 + 0x23 + 0x22 +0x21 + 0x20
=128+0+0+0+0+4+0+0=132
Mantissa bits M:11110110110011001100110B = 8087142
Step 2:
Calculate the decimal number
D = (-1) x (1.0 + M/223) x 2E-127
= (-1) x (1.0 + 8087142/223) x 2132-127
= 1 x 1.964062452316284 x 32
= 62.85
Reference code:
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))*
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)