Hy,
I'm trying to convert a float number to a uint16_t[2], according the IEEE 754 format.
For example, I manage to do the contrary :
double uint16t2double(uint16_t* buff)
{
uint32_t test32 = 0;
test32 = buff[0];
int i;
double resultat;
test32 = test32 << 16;
test32 = test32 + buff[1];
char sign;
signe = test32 >> 31;
unsigned char expo;
expo = (test32 & 0x7F800000) >> 23;
expo -= 127;
uint32_t mantissa = (test32 & 0x7FFFFF);
float m = 1;
for(i=1; i<=23; i++) {
if((mantissa & 0x400000) != 0) {
m += pow(2, -i);
}
mantissa = mantissa << 1;
}
result = pow((double)2,(double)expo);
if(sign)
result = -1*result * m;
else
result = result * m;
return result;
}
double uint16t2double(uint16_t* buff)
{
uint32_t test32 = 0;
test32 = buff[0];
int i;
double resultat;
test32 = test32 << 16;
test32 = test32 + buff[1];
char sign;
signe = test32 >> 31;
unsigned char expo;
expo = (test32 & 0x7F800000) >> 23;
expo -= 127;
uint32_t mantissa = (test32 & 0x7FFFFF);
float m = 1;
for(i=1; i<=23; i++) {
if((mantissa & 0x400000) != 0) {
m += pow(2, -i);
}
mantissa = mantissa << 1;
}
result = pow((double)2,(double)expo);
if(sign)
result = -1*result * m;
else
result = result * m;
return result;
}
To copy to clipboard, switch view to plain text mode
Can anyone help me ?
PS: I'm new on forum, please excuse me if my message is not in the proper section and my explanations too succinct.
Bookmarks