title: c++ read (big endian) binary float
body: <p>I have a binary file that contains n single-precision values. I know that the format used when writing data was big-endian. When I read in the data into a float vector ('mainvector' in the code below), by default, the data is read in according to little-endian format. I use the following to read data:</p>
  
  <pre><code>ifstream inputfile("filepath",ifstream::in|ifstream::binary)
  inputfile.read(reinterpret_cast&lt;char*&gt;(&amp;mainvector[0]), n*4);
  inputfile.close()
  </code></pre>
  
  <p>There is a lot of discussion on endianness and conversion on stackoverflow itself. However, this is the first time I have to deal with the endianness issue and all the information available is a bit overwhelming. In the process I learned that the bytes are reversed (big vs. little endian).</p>
  
  <p>Is there a one-liner that I can incorporate to change the default little-endian treatment of my binary data to big-endian, or post-process my <em>mainvector</em> in order to get the original data ? or would I need to manually reverse byte-order of each of the n values ?</p>
  
  <p><strong>UPDATE</strong>: It appears that there is no one-liner for this. Individually changing the byte order is the way to do it! How to do that is discussed here by Mats and elsewhere (e.g. <a href="http://www.gamedev.net/page/resources/_/technical/game-programming/writing-endian-independent-code-in-c-r2091" rel="nofollow">here</a>).</p>