#include <iostream>
#include <cstring>
using namespace std;
int main()
{
unsigned char bytes[] = {0x3F, 0xA0, 0x00, 0x00};
uint32_t x = bytes[0];
for (int i = 1; i < std::size(bytes); ++i) x = (x << 8) | bytes[i];
static_assert(sizeof(float) == sizeof(uint32_t), "Float and uint32_t size dont match. Check another int type");
float f{};
memcpy(&f, &x, sizeof(x));
// or since C++20 if available: float f = std::bit_cast<float>(x)
cout << "f = " << f << endl;
}
Live
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…