解包十六进制编码的浮点数

dbr*_*dbr 6 c++ floating-point hex

我正在尝试将以下Python代码转换为C++:

import struct
import binascii


inputstring = ("0000003F" "0000803F" "AD10753F" "00000080")
num_vals = 4

for i in range(num_vals):
    rawhex = inputstring[i*8:(i*8)+8]

    # <f for little endian float
    val = struct.unpack("<f", binascii.unhexlify(rawhex))[0]
    print val

    # Output:
    # 0.5
    # 1.0
    # 0.957285702229
    # -0.0
Run Code Online (Sandbox Code Playgroud)

因此,它读取32位值的十六进制编码字符串,使用该unhexlify方法将其转换为字节数组,并将其解释为little-endian浮点值.

以下几乎可以工作,但代码有点蹩脚(最后00000080解析不正确):

#include <sstream>
#include <iostream>


int main()
{
    // The hex-encoded string, and number of values are loaded from a file.
    // The num_vals might be wrong, so some basic error checking is needed.
    std::string inputstring = "0000003F" "0000803F" "AD10753F" "00000080";
    int num_vals = 4;


    std::istringstream ss(inputstring);

    for(unsigned int i = 0; i < num_vals; ++i)
    {
        char rawhex[8];

// The ifdef is wrong. It is not the way to detect endianness (it's
// always defined)
#ifdef BIG_ENDIAN
        rawhex[6] = ss.get();
        rawhex[7] = ss.get();

        rawhex[4] = ss.get();
        rawhex[5] = ss.get();

        rawhex[2] = ss.get();
        rawhex[3] = ss.get();

        rawhex[0] = ss.get();
        rawhex[1] = ss.get();
#else
        rawhex[0] = ss.get();
        rawhex[1] = ss.get();

        rawhex[2] = ss.get();
        rawhex[3] = ss.get();

        rawhex[4] = ss.get();
        rawhex[5] = ss.get();

        rawhex[6] = ss.get();
        rawhex[7] = ss.get();
#endif

        if(ss.good())
        {
            std::stringstream convert;
            convert << std::hex << rawhex;
            int32_t val;
            convert >> val;

            std::cerr << (*(float*)(&val)) << "\n";
        }
        else
        {
            std::ostringstream os;
            os << "Not enough values in LUT data. Found " << i;
            os << ". Expected " << num_vals;
            std::cerr << os.str() << std::endl;
            throw std::exception();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

(在OS X 10.7/gcc-4.2.1上编译,简单g++ blah.cpp)

特别是,我想摆脱BIG_ENDIAN宏观内容,因为我确信有更好的方法可以做到这一点,正如本文所讨论的那样.

很少有其他随机细节 - 我不能使用Boost(项目的依赖性太大).该字符串通常包含1536(8 3*3)和98304浮点值(32 3*3),最多786432(64 3*3)

(edit2:添加了另一个值,00000080== -0.0)

dbr*_*dbr -1

这就是我们最终得到的结果,OpenColorIO/src/core/FileFormatIridasLook.cpp

(Amardeep 的未签名uint32_t修复的答案也可能有效)

    // convert hex ascii to int
    // return true on success, false on failure
    bool hexasciitoint(char& ival, char character)
    {
        if(character>=48 && character<=57) // [0-9]
        {
            ival = static_cast<char>(character-48);
            return true;
        }
        else if(character>=65 && character<=70) // [A-F]
        {
            ival = static_cast<char>(10+character-65);
            return true;
        }
        else if(character>=97 && character<=102) // [a-f]
        {
            ival = static_cast<char>(10+character-97);
            return true;
        }

        ival = 0;
        return false;
    }

    // convert array of 8 hex ascii to f32
    // The input hexascii is required to be a little-endian representation
    // as used in the iridas file format
    // "AD10753F" -> 0.9572857022285461f on ALL architectures

    bool hexasciitofloat(float& fval, const char * ascii)
    {
        // Convert all ASCII numbers to their numerical representations
        char asciinums[8];
        for(unsigned int i=0; i<8; ++i)
        {
            if(!hexasciitoint(asciinums[i], ascii[i]))
            {
                return false;
            }
        }

        unsigned char * fvalbytes = reinterpret_cast<unsigned char *>(&fval);

#if OCIO_LITTLE_ENDIAN
        // Since incoming values are little endian, and we're on little endian
        // preserve the byte order
        fvalbytes[0] = (unsigned char) (asciinums[1] | (asciinums[0] << 4));
        fvalbytes[1] = (unsigned char) (asciinums[3] | (asciinums[2] << 4));
        fvalbytes[2] = (unsigned char) (asciinums[5] | (asciinums[4] << 4));
        fvalbytes[3] = (unsigned char) (asciinums[7] | (asciinums[6] << 4));
#else
        // Since incoming values are little endian, and we're on big endian
        // flip the byte order
        fvalbytes[3] = (unsigned char) (asciinums[1] | (asciinums[0] << 4));
        fvalbytes[2] = (unsigned char) (asciinums[3] | (asciinums[2] << 4));
        fvalbytes[1] = (unsigned char) (asciinums[5] | (asciinums[4] << 4));
        fvalbytes[0] = (unsigned char) (asciinums[7] | (asciinums[6] << 4));
#endif
        return true;
    }
Run Code Online (Sandbox Code Playgroud)