三亩地 三亩地SAN MU DI · CODE DIARY
ARTICLE DETAIL

日记详情

真实记录编程学习的某一天,欢迎挑你感兴趣的翻一翻。

union 实现char转float GPS精度不丢失

union 实现char转float GPS精度不丢失

由于GPS经纬度传过来的分别是4个char, 需要转换float

//union内存共享地址实现,较好 #include <iostream> #include <stdlib.h> #include <string.h> using namespace std; union CharFloat { unsigned char str[4]; float value; }; int main(void) { union CharFloat F; F.value = 0.0; F.str[0] = 0x00; //高字节放在高地址 F.str[1] = 0x00; F.str[2] = 0xF0; F.str[3] = 0x41; printf("%.4f\n",F.value); //printf("%.15E\n",F.value); //127*1e7 return 0; }

union 共享内存地址是一个较好的解决char转float方案

← 返回列表