Day: September 23, 2017

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;
}