title: read float and double from binary data in C++
body: <p>I need to be able to read in a float or double from binary data in C++, similarly to Python's <code>struct.unpack</code> function.  My issue is that the data I am receiving will always be big-endian.  I have dealt with this for integer values <a href="https://stackoverflow.com/questions/13863667/dealing-with-endianness-in-c/13864024#comment19100561_13864024">as described here</a>, but working byte by byte does not work with floating point values.  I need a way to extract floating point values (both 32 bit <code>float</code>s and 64 bit <code>double</code>s) in in C++, similar to how you would use <code>struct.unpack("&gt;f", num)</code> or <code>struct.unpack("&gt;d", num)</code> in Python.</p>
  
  <p>here's an example of what I have tried:</p>
  
  <pre><code>stuct.unpack("d", num) ==&gt; *(double*) str; // if str is a char* containing the data
  </code></pre>
  
  <p>That works fine if <code>str</code> is little-endian, but not if it is big-endian, as I know it will always be.  The problem is that I do not know what the native endianness of the environment will be, so I need to be able to extract the binary data as big-endian at all times.</p>
  
  <p>If you look at the linked question, you'll see this is easily using bitwise-ors and bitshifts for integer values, but that method does not work for floating point.</p>
  
  <p><strong>NOTE</strong> I should have pointed this out earlier, but I cannot use c++11 or any third party libraries other than Boost.</p>