i cant really find a definitive answer as to how to go from 8 bytes into a double, i have tried two methods based on my limited knowlege and experience of what what works with longs and floats, but these methods don't seem to work for double.
my goal is to get data that looks like a GPS reading something like "30.111111" and "-29.000000" (my office in South Africa), and the actual data received is also shown in the example below. if i can just get the double conversion to work on a plain simple 8 bytes then i can fiddle around the GPS bytes with more confidence.
here is the sketch and two methods, both work for long but none seem to work for double!!!
my goal is to get data that looks like a GPS reading something like "30.111111" and "-29.000000" (my office in South Africa), and the actual data received is also shown in the example below. if i can just get the double conversion to work on a plain simple 8 bytes then i can fiddle around the GPS bytes with more confidence.
here is the sketch and two methods, both work for long but none seem to work for double!!!
Code:
// pass in 4 bytes returns a long
long BytesToLong(byte b0, byte b1, byte b2, byte b3){
long iresult = b0;
iresult = iresult + 256 * b1;
iresult = iresult + 256 *256 * b2;
iresult = iresult + 256 * 256 * 256 * b3;
return iresult;
}
//pass in 4 bytes returns a long uses union method
long BytesTolu(byte b0, byte b1, byte b2, byte b3){
union u_tag { byte b[4]; long fval;} u; // temporary variable to convert bytes to long
u.b[0] =b0;
u.b[1] = b1;
u.b[2] = b2;
u.b[3] = b3;
return u.fval;
}
//pass in 8 bytes returns a double uses union method
double BytesTodu(byte b0, byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7){
union u_tag { byte b[8]; double fval;} u; // temporary variable to convert bytes to double
u.b[0] =b0;
u.b[1] = b1;
u.b[2] = b2;
u.b[3] = b3;
u.b[4] = b4;
u.b[5] = b5;
u.b[6] = b6;
u.b[7] = b7;
return u.fval;
}
//pass in 8 bytes returns a double
double BytesToDouble(byte b0, byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7){
double iresult = b0;
iresult = iresult + 256 * b1;
iresult = iresult + 256 *256 * b2;
iresult = iresult + 256 * 256 * 256 * b3;
iresult = iresult + 256 * 256 * 256 * 256 * b4;
iresult = iresult + 256 * 256 * 256 * 256 * 256 * b5;
iresult = iresult + 256 * 256 * 256 * 256 * 256 * 256 * b6;
iresult = iresult + 256 * 256 * 256 * 256 * 256 * 256 * 256 * b7;
return iresult;
}
void setup()
{
Serial.begin (115200);
delay(500);// short delay to allow com port to initialise
Serial.println ("starting...");
// testing long
byte b[4] = {0,0,0,1}; // our byte array recevived from some sensor
long iresult = BytesToLong(b[0],b[1],b[2],b[3]);
Serial.print("Long using multip:"); Serial.println(iresult);
iresult = BytesTolu(b[0],b[1],b[2],b[3]);
Serial.print("Long using union:"); Serial.println(iresult);
// testing double
byte d[8] = {0,0,0,1,0,0,0,0}; // an 8 byte array recevived from some other sensor
double dresult = BytesToDouble(d[0],d[1],d[2],d[3],d[4],d[5],d[6],d[7]);
Serial.print("double using multip:"); Serial.println(dresult);
dresult = BytesTodu(d[0],d[1],d[2],d[3],d[4],d[5],d[6],d[7]);
Serial.print("double using union:"); Serial.println(dresult);
// actual readings from an actual sensor
byte longitude[8] = {107 ,80 ,12 ,26, 2, 163, 224, 191};
byte latitude[8] = {74, 218 ,254 ,192 ,140 ,45, 225 ,63};
double longi, lati;
longi = BytesToDouble(longitude[0], longitude[1],longitude[2],longitude[3],longitude[4],longitude[5],longitude[6],longitude[7]);
Serial.print("longitude:"); Serial.println(longi);
// longi should be 31.0
lati = BytesToDouble(latitude[0], latitude[1],latitude[2],latitude[3],latitude[4],latitude[5],latitude[6],latitude[7]);
Serial.print("latitude:"); Serial.println(lati);
// latti should be -29.0
}