I'm trying to convert Lat and Lon coordinates to 4 decimal place float. I have 'working' code, but it's just a bit off like %50 of the time. Can anyone help me or have a better design to make it more exact?
float sexag2decimal(char * deg){
char *sdeg = malloc(sizeof(char)*3);
char *smin = malloc(sizeof(char)*3);
char *ssec = malloc(sizeof(char)*3);
char *ssec2 = malloc(sizeof(char)*5);
int size = strlen(deg);
int m = 0;
for(m = 0; m < size-1; m++){
if(deg[m] >= 65 && deg[m] <= 122){
return 0;
}
}
float converted = 0;
if(deg[0] == '0'){
strcpy(&sdeg[0], °[1]);
strcpy(&sdeg[1], °[2]);
strcpy(&smin[0], °[4]);
strcpy(&smin[1], °[5]);
strcpy(&ssec[0], °[7]);
strcpy(&ssec[1], °[8]);
strcpy(&ssec2[0], °[10]);
strcpy(&ssec2[1], °[11]);
strcpy(&ssec2[2], °[12]);
strcpy(&ssec2[3], °[13]);
}else{
if(deg[14] == 'W')
return 0;
strcpy(&sdeg[0], °[0]);
strcpy(&sdeg[1], °[1]);
strcpy(&smin[0], °[3]);
strcpy(&smin[1], °[4]);
strcpy(&ssec[0], °[6]);
strcpy(&ssec[1], °[7]);
strcpy(&ssec2[0], °[9]);
strcpy(&ssec2[1], °[10]);
strcpy(&ssec2[2], °[11]);
strcpy(&ssec2[3], °[12]);
}
sdeg[2] = '\0';
smin[2] = '\0';
ssec[2] = '\0';
ssec2[4] = '\0';
converted = atoi(sdeg) + ((float)atoi(smin)/60.0) + (((float)atoi(ssec)+((float)atoi(ssec2))/10000)/3600.0);
free(sdeg);
free(smin);
free(ssec);
free(ssec2);
return converted;
}
Thanks!
Input:
30-25-30.7140N, 086-53-37.8590W
29-57-33.3000N,081-20-23.0000W
My output:
30.4252,-86.8939
29.9592,-81.3397
Correct output:
30.4252,-86.8938
29.9593,-81.3397
Thanks!