Skip to content

Instantly share code, notes, and snippets.

@LightningStalker
Last active August 27, 2025 12:55
Show Gist options
  • Select an option

  • Save LightningStalker/20e6a30e3328e7e673cef38606e9b55f to your computer and use it in GitHub Desktop.

Select an option

Save LightningStalker/20e6a30e3328e7e673cef38606e9b55f to your computer and use it in GitHub Desktop.
Convert decimal GPS coordinates into sexagesimal
/* Compile with gcc -Wall -o dec2sex dec2sex.c -lm */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main (int argc, char **argv)
{
int d, m, s, ms;
double mm, ss;
/* prevent segfault in case no args */
if (argc > 1)
{
d = floor(atoi(argv[1]));
}
/* continue algo */
switch (argc)
{
case 2:
/* decimal pure */
m = floor(mm = (atof(argv[1]) - d) * 60.0);
break;
case 3:
/* decimal minutes */
m = floor(mm = atof(argv[2]));
break;
default:
/* usage */
puts ("\n dec2sex is a decimal to sexagesimal (base 60) converter.");
puts (" dec2sex converts decimal degree coordinates into sexagesimal deg/m/s/mils");
puts (" use ABSOLUTE VALUE of any negative number, and garbage in = garbage out");
puts (" output is the coordinates in base 60");
puts (" dec2sex is not sexy.\n");
puts (" Usage: dec2sex XX.XXXXXX (where X is some number)");
puts (" Or: dec2sex XX XX.XXXXXX (for decimal minutes)");
puts (" Example: dec2sex 43.923056");
puts (" Output should be: 43°55\'23\"2ms");
puts (" 43°55.383362\'\n");
printf("%i", argc);
return (1);
break;
}
/* seconds and milli */
s = floor(ss = (mm - m) * 60.0);
ms = round((ss - s) * 1000.0); // last one get rounded instead
/* out姤put */
printf ("%i°%i\'%i\"%ims\n", d, m, s, ms);
if (argc != 3)
{
/* we aready has it */
printf ("%i°%f\'\n", d, mm);
}
return (0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment