#include<iostream>
using namespace std;
void FloatToBuf(float num, unsigned char buf[4]);
float BufToFloat(unsigned char buf[4]);
int main()
{
float f = 639.5f; //待转换的浮点数
int *p = reinterpret_cast<int *>(&f);
for(int i = 31; i >= 0; i--) //原始的二进制位
{
cout << (*p >> i & 1);
if(i % 8 == 0)
{
cout << ' ';
}
}
cout << endl;
unsigned char buf[4];
FloatToBuf(f, buf);
for(i = 0; i < 32; i++) //转换后的大端模式二进制位
{
cout << (buf[i / 8] >> (7 - i % 8) & 1);
if(i % 8 == 7)
{
cout << ' ';
}
}
cout << endl;
cout << BufToFloat(buf) << endl; //再转回去的浮点数
return 0;
}
void FloatToBuf(float num, unsigned char buf[4])
{
float item = 0.5f;
int index = 0, i;
if(num ==
C++解析IEEE754中float格式的数据
最新推荐文章于 2023-06-12 08:30:00 发布