C/C++ - Convert 24-bit signed integer to float

前端 未结 7 2111
野的像风
野的像风 2021-01-12 20:27

I\'m programming in C++. I need to convert a 24-bit signed integer (stored in a 3-byte array) to float (normalizing to [-1.0,1.0]).

The platform is MSVC++ on x86 (wh

相关标签:
7条回答
  • 2021-01-12 21:05

    Works for me:

    float convert(const char* stream)
    {
        int fromStream = 
            (0x00 << 24) + 
            (stream[2] << 16) + 
            (stream[1] << 8) + 
             stream[0];
    
        return (float)fromStream;
    }
    
    0 讨论(0)
提交回复
热议问题