long struct problem

24 Sep 2010

This is my code:

#include "mbed.h"



Serial pc(USBTX, USBRX);  // USBTX - Tranmit on USB  USBRX - receive on USB
Serial gps(p9, p10);  // tx, rx

char Buffer[4];

struct Location {
    long lat;                // Lattitude * 10**7
    long lng;                // Longitude * 10**7
    long alt;                // Altitude in centimeters (meters * 100)
};
struct Location current_loc        = {0,0,0};


union long_union {
    long dword;
    char byte[4];
} longUnion;


long join_4_bytes(char Buffer[])
{
    longUnion.byte[3]= *Buffer;
    longUnion.byte[2]= *(Buffer+1);
    longUnion.byte[1]= *(Buffer+2);
    longUnion.byte[0]= *(Buffer+3);
    return(longUnion.dword);
}

int main(){
Buffer[0]=0x01;
Buffer[1]=0x60;
Buffer[2]=0x74;
Buffer[3]=0xCC;
current_loc.lat=join_4_bytes(&Buffer[0]);
pc.printf("latitud: %.f\n\r",current_loc.lat);
}

the output:

latitud: 0

My problem is that I need "current_loc.lat" to be float and I don`t know how to do it.

If I put:

pc.printf("latitud: %d\n\r",current_loc.lat);
I get:

latitud: 23098572

Thaks in advance!

Jose

25 Sep 2010

I am stupid, jajaja!! It was late and I was tired. Problem solved:

struct Location {
    float lat;                // Lattitude * 10**7 
    float lng;                // Longitude * 10**7 
    float alt;                // Altitude in centimeters (meters * 100)
};
struct Location current_loc        = {0,0,0};