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

前端 未结 7 2109
野的像风
野的像风 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:04

    Since you are using a char array, it does not necessarily follow that the input is little endian by virtue of being x86; the char array makes the byte order architecture independent.

    Your code is somewhat over complicated. A simple solution is to shift the 24 bit data to scale it to a 32bit value (so that the machine's natural signed arithmetic will work), and then use a simple ratio of the result with the maximum possible value (which is INT_MAX less 256 because of the vacant lower 8 bits).

    #include 
    
    float convert(const unsigned char* src)
    {
        int i = src[2] << 24 | src[1] << 16 | src[0] << 8 ;
        return i / (float)(INT_MAX - 256) ;
    }
    

    Test code:

    unsigned char* makeS24( unsigned int i, unsigned char* s24 )
    {
        s24[2] = (unsigned char)(i >> 16) ;
        s24[1] = (unsigned char)((i >> 8) & 0xff);
        s24[0] = (unsigned char)(i & 0xff);
        return s24 ;
    }
    
    #include 
    
    int main()
    {
        unsigned char s24[3] ;
        volatile int x = INT_MIN / 2 ;
    
        std::cout << convert( makeS24( 0x800000, s24 )) << std::endl ;  // -1.0
        std::cout << convert( makeS24( 0x7fffff, s24 )) << std::endl ;  //  1.0
        std::cout << convert( makeS24( 0, s24 )) << std::endl ;         //  0.0
        std::cout << convert( makeS24( 0xc00000, s24 )) << std::endl ;  // -0.5
        std::cout << convert( makeS24( 0x400000, s24 )) << std::endl ;  //  0.5
    
    }
    

提交回复
热议问题