[英]Converting a string to a double or float to execute math equation
I have a function that I am trying to convert from PHP. 我有一个要从PHP转换的函数。 I am executing the function like this example:
我正在执行类似此示例的功能:
latlong_hex("1234.5678N");
and I need the result to be processed but I am running into problems with the string to double conversion and the calculations. 我需要处理结果,但是我遇到了将字符串加倍转换和计算的问题。 After the conversion, I am losing all numbers after the decimal.
转换后,我将丢失小数点后的所有数字。 The HEX function at the bottom is working fine.
底部的十六进制功能正常运行。
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;
}
This: 这个:
free(gps_latlong);
is instant undefined behavior, since gps_latlong
is not a heap-allocated object. 是即时未定义行为,因为
gps_latlong
不是堆分配的对象。 Remove that line. 删除该行。
Also, you should call strtod()
directly on gps_coord
, the local copying serves no purpose. 另外,您应该直接在
gps_coord
上调用strtod()
,本地复制没有任何作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.