使用 union 从双精度转换为十六进制再转换为双精度 [英] Converting from double to hexadecimal back to double using union

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

问题描述

我什至不确定我的标题是否准确,但对于我的项目,我需要取一个 double 并将其转换为十六进制,然后将其转换回 double.但是为了做到这一点,我想我需要重载运算符 <<在我可以输出答案之前.到目前为止,这是我所拥有的,但我收到提示没有运算符<<"匹配这些操作数">有人可以指出我应该如何重载<<<<<<的正确方向.运算符主要?谢谢!

I am not even sure if my title is accurate, but for my project, I need to take a double and convert it into a hexadecimal and then convert it back to a double. But in order to do that, I think I would need to overload the operator << before I can output the answer. This is what I have so far, but I get prompted that "no operator "<<" match these operands"> Could someone please point me in the right direction of how I should overload the << operator in main? Thank you!

#include <iostream>
#include <iomanip>
#include <cstdint>

using namespace std;

void out_char_as_hex(int c)
{
    cout << hex << setw(2) << setfill('0') << c;
}


int main()
{
    union { double d; uint64_t u; } tmp;
    double x = -0.15625;
    tmp.d = x;

    cout << out_char_as_hex(tmp.d) << endl;

    return 0;
}

如果有帮助,这是一个问题如果 x 是 double 类型的变量,它的二进制表示可以重新解释为 64 位整数,可以精确表示.为此,您可以获取内存对 double 类型的变量的地址并重新解释将其强制​​转换为 64 位 int 指针或使用联合.为了使文本表示更紧凑,我们使用基数 16(十六进制).例如,应保存双精度数 -0.15625将文件作为 16 个字符的序列 bfc4000000000000(参见 DemoHex.cpp 中的示例,使用联合).读取时需要读取整数以十六进制格式保存并将其重新解释为双精度.需要修改operator<<的实现双人并实现运算符重载>>"

if it helps this is the question "If x is a variable of type double, it binary representation can be re-interpreted as a 64 bits integer, which can be represented exactly. To do that, you can either get the memory address to the variable of type double and reinterpret cast it to a 64-bit int pointer or use a union. To make the textual representation more compact, we use base 16 (hexadecimal). For example, the double number -0.15625 should be saved to file as the sequence of 16 characters bfc4000000000000 (see example in DemoHex.cpp, which use a union). When reading, you need to read the integer number saved in hexadecimal format and re-interpret it as a double. You need to modify the implementation of the operator<< for double and implement the overload of the operator>>"

推荐答案

这是一个不使用联合的版本,而是将位模式从 double 复制到 uint64_t.

This is a version that doesn't use a union, but instead copies the bit pattern from a double to a uint64_t.

假设这个位模式对一个整数有效,那么复制也应该有效.它也产生了预期的输出.

Assuming that this bit pattern is valid for an integer, the copying should be valid too. And it also produces the expected output.

#include <iostream>
#include <iomanip>
#include <cstdint>

using namespace std;

void out_char_as_hex(std::uint64_t c)
{
    cout << hex << setw(16) << setfill('0') << c << endl;
}


int main()
{
    uint64_t u;
    double x = -0.15625;

    std::memcpy(&u, &x, sizeof(u));

    out_char_as_hex(u);

    return 0;
}

然而,这不是教授要求的解决方案,所以可能错误".

However, this isn't the solution the professor has requested, so probably "wrong".

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

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