参考资料:
1.http://blog.csdn.net/delphiwcdj/article/details/4649854/。
2.http://bbs.csdn.net/topics/300043880。
3.http://blog.csdn.net/dengjianqiang2011/article/details/8749176。
问题:
有时候在传输过程中会把浮点数使用如下形式存储为二进制形式:
memcpy(ibuf+4, &f_c0, sizeof(float));
memcpy(ibuf+4+4, &f_c1, sizeof(float));
在另一端怎么将十六进制形式的数据转为浮点数呢?以下是简单实现:
#include <stdio.h>
#include <string.h>
#define SIZE 10
int main()
{
union NUM{
unsigned int ss;
float ff;
};
char str[SIZE]="0x11";
union NUM aa;
float result;
scanf("%s",str);
//printf("%s\n",str);
sscanf(str,"%x",&aa.ss);//
//printf("%x\n",aa.ss);
result = aa.ff;
printf("result:%f\n",result);
return 0;
}