转换十六进制到双十六进制? [英] Convert Hex to Double to Hex?

查看:305
本文介绍了转换十六进制到双十六进制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my project I have a QString with the hex value (Big Endian)

QString hex_in("413DF3EBA463B0");

如何将hex_in转换为舍入的double? IEEE 754( https://en.wikipedia.org/wiki/Double_precision_floating-point_format

How could I convert hex_in to a rounded double? IEEE 754 (https://en.wikipedia.org/wiki/Double_precision_floating-point_format)

34.5

用户将编辑double,然后我的程序需要将其转换回hex。

The user will edit the double and then my program needs to convert it back to hex.

感谢您的时间:)

Thanks for your time :)

推荐答案

实际上只有一种方法可以做到这一点,那就是将字符串转换为一个整数,把它放在一个 union 在那里你设置一个整数成员并读出一个 double 成员。

There is really only one way to do it, and that is to convert the string to an integer, put it in a union where you set an integer member and read out a double member.

字符串转换你可以使用例如其中一个功能

For the string conversion you can use e.g. one of these functions.

示例代码:

Example code:

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';

以上代码的输出将是

The output of the code above will be


413DF3EBA463B0 = 1.91824e-307

这篇关于转换十六进制到双十六进制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆