SparkFun Forums 

Where electronics enthusiasts find answers.

All things pertaining to wireless and RF links
By cesarhid
#4395
I get from a Lassen iQ TSIP data like this:

4A 3E DD D7 88 BF EF FF E6 41 BC 4D 7A 47 9F 8B C3 48 F6 5A 00

I know this:
4A is packet indicating GPS position fix.
3E DD D7 88 is a single float number indicating Latitude.
3E DD D7 88 in binary is 00111110 11011101 11010111 10001000 and that I need to extract from here sign bit, exponent and fraction so I can calculate the single float number, but I can't find how.

I need to know how I get the single float number from those 4 bytes.

Sorry if my english is not good.
By Lajon
#4404
You don't say what development environment you have but I hope this can be useful
Code: Select all
double ieeeSingle(uint8_t *p)
{
#if 1
	union  {
       float f;
	   uint8_t b[4];
	} fConv;
	fConv.b[3] = p[0];
	fConv.b[2] = p[1]; 
	fConv.b[1] = p[2];
	fConv.b[0] = p[3];
	return fConv.f;
#else
	uint8_t e = (p[0] << 1) | (p[1] >> 7);
	uint32_t f = (((uint32_t)p[1]&0x7F) << 16) | ((uint32_t)p[2] << 8) | ((uint32_t)p[3]);
	return ((p[0] & 0x80) ? -1.0 : 1.0) * pow(2.0, e - 127) * (1.0 + ((float)f/8388608.0));
#endif
}
Note that there are 2 alternatives. The first one:
* can be used in a C development environment that has IEEE Single as the native float format.
* depends on byte order (so you may have to reverse the assignments - fConv.b[0] = p[0], fConv.b[1] = p[1] etc).

The second method computes the number from the bits (actually for a subset of valid IEEE single numbers only). You can read about the format here:
http://www.psc.edu/general/software/pac ... /ieee.html

/Lars