我有一个函数,我试图从PHP转换。我正在执行这个函数,如下所示:
latlong_hex("1234.5678N");
我需要处理结果,但我遇到了字符串到双倍转换和计算的问题。转换后,我丢失了小数点后的所有数字。底部的十六进制函数工作正常。
int latlong_hex(char* gps_coord)
{
int gps_result;
char direction[2] = {0};
char gps_latlong[10] = {0};
double latdeg;
double tempDec;
char* tempGPS;
strncpy(gps_latlong, gps_coord, 9);
strncpy(direction, gps_coord+9, 1);
tempDec = strtod(gps_latlong, NULL);
free(gps_latlong);
tempDec = tempDec / 100;
if(direction == 'W' || direction == 'S')latdeg = round((floor(tempDec)+((tempDec - floor(tempDec))/60),7))*-1;
else latdeg = round((floor(tempDec) + ((tempDec - floor(tempDec))/60),7));
if(latdeg > 0){
gps_result = latdeg / 0.0000001;
}
else{
gps_result = (4294967295 + (latdeg/0.0000001)) ;
}
dec_hex(gps_result);
return 1;
}
发布于 2017-04-10 20:19:05
这一点:
free(gps_latlong);
是即时未定义的行为,因为gps_latlong
不是堆分配的对象。去掉那行。
此外,您应该直接在gps_coord
上调用strtod()
,本地复制没有任何作用。
https://stackoverflow.com/questions/43322924
复制相似问题