将Hex转换为Double到Hex?

mrg*_*g95 1 c++ floating-point double qt hex

在我的项目中,我有一个带有十六进制值的QString(Big Endian)

QString hex_in("413DF3EBA463B0");
Run Code Online (Sandbox Code Playgroud)

我怎么能将hex_in转换为圆角双?IEEE 754(https://en.wikipedia.org/wiki/Double_precision_floating-point_format)

34.5
Run Code Online (Sandbox Code Playgroud)

用户将编辑double,然后我的程序需要将其转换回十六进制.

谢谢你的时间 :)

Som*_*ude 5

实际上只有一种方法,即将字符串转换为整数,将其放在union设置整数成员的位置并读出double成员.

对于字符串转换,您可以使用例如这些函数之一.


示例代码:

double hexstr2double(const std::string& hexstr)
{
    union
    {
        long long i;
        double    d;
    } value;

    value.i = std::stoll(hexstr, nullptr, 16);

    return value.d;
}

// ...

std::cout << "413DF3EBA463B0 = " << hexstr2double("413DF3EBA463B0") << '\n';
Run Code Online (Sandbox Code Playgroud)

上面代码的输出将是

413DF3EBA463B0 = 1.91824e-307