# Converting Latitude to 32 bits

```/*
* Convert Latitude in DMS (Degrees Minutes Seconds) to 32-bit integer
* between -162000000 and 162000000
*/

#include < stdio.h>

int main(void)
{
int degree;
int min;
double sec;
double frac_sec;

printf("Inform the latitude in this format XXº YY\' ZZ.ABCD\"\n");

printf("Type the value of the Degree (XX): ");
scanf("%d", &degree);

if (degree < -90 || degree > 90)
{
printf("The Degree value needs to be between -90 and 90!\n");
return -1;
}

printf("Type the value of the Minute (YY): ");
scanf("%d", &min);

if (min < 0 || min > 59)
{
printf("The Minute value needs to be between 0 and 59!\n");
return -1;
}

printf("Type the value of the Seconds (ZZ.ABCD): ");
scanf("%lf", &sec);

if (sec < 0 || sec >= 60)
{
printf("The Seconds value needs to be between 0 e 59.9999!\n");
return -1;
}

printf("%02dº %02d\' %02f\"\n", degree, min, sec);

/* Convert everything to seconds and add fraction of seconds */

frac_sec  = degree * 60 * 60;
frac_sec += (min * 60);
frac_sec += sec;

/* Multiply by 500 because the unit is in 1/500 seconds */

frac_sec = frac_sec * 500;

printf("Value converted = %d => 0x%08X!\n", (int) frac_sec, (int) frac_sec);

return 0;
}
```

## 2 thoughts on “Converting Latitude to 32 bits”

1. Franz says:

I’ve always done it this way: Decimal deg = degrees + (minutes / 60) + (seconds / 3600)

2. acassis says:

Hi Franz, right! The way to do is correct, this is the same way this guy do: https://www.youtube.com/watch?v=S_nxWwtYfq4 The issue with this approach is the float resolution. You need to use double (64-bit) instead of float (32-bit) to store your decimal degree up to the fourth decimal place, because otherwise you will lose resolution. This conversion to 32 bits will not lose resolution (at least up to four decimal places). I did many tests to understand why some tracking protocols was doing this conversion.