您的位置:首页 > 其它

16进制字符串转化为浮点数

2014-03-05 18:22 316 查看
参考资料

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: