Converting 4 raw bytes into 32-bit floating point

后端 未结 3 1252
有刺的猬
有刺的猬 2020-12-11 23:47

I\'m trying to re-construct a 32-bit floating point value from an eeprom.

The 4 bytes in eeprom memory (0-4) are : B4 A2 91 4D

and the PC (VS Studio) recon

3条回答
  •  醉酒成梦
    2020-12-12 00:13

    You need to cast at the pointer level.

    int     myFourBytes = /* something */;
    float*  myFloat = (float*) &myFourBytes;
    cout << *myFloat;
    

    Should work.

    If the data is generated on a different platform that stores values in the opposite endianness, you'll need to manually swap the bytes around. E.g.:

    unsigned char myFourBytes[4] = { 0xB4, 0xA2, 0x91, 0x4D };
    std::swap(myFourBytes[0], myFourBytes[3]);
    std::swap(myFourBytes[1], myFourBytes[2]);
    

提交回复
热议问题