title: Converting from double to hexadecimal back to double using union
body: <p>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 &lt;&lt; before I can output the answer. This is what I have so far, but I get prompted that "no operator "&lt;&lt;" match these operands"> Could someone please point me in the right direction of how I should overload the &lt;&lt; operator in main? Thank you!</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;iomanip&gt;
  #include &lt;cstdint&gt;
  
  using namespace std;
  
  void out_char_as_hex(int c)
  {
      cout &lt;&lt; hex &lt;&lt; setw(2) &lt;&lt; setfill('0') &lt;&lt; c;
  }
  
  
  int main()
  {
      union { double d; uint64_t u; } tmp;
      double x = -0.15625;
      tmp.d = x;
  
      cout &lt;&lt; out_char_as_hex(tmp.d) &lt;&lt; endl;
  
      return 0;
  }
  </code></pre>
  
  <p>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&lt;&lt; for double
  and implement the overload of the operator>>"</p>