title: Detect endianness of binary file data
body: <p>Recently I was (again) reading about 'endian'ness. I know how to identify the endianness of host, as there are lots of post on SO, and also I have seen <a href="https://www.ibm.com/developerworks/aix/library/au-endianc/" rel="noreferrer">this</a>, which I think is pretty good resource.</p>
  
  <p>However, one thing I like to know is to how to detect the endianness of input binary file. For example, I am reading a binary file (using <code>C++</code>) like following:</p>
  
  <pre><code>ifstream mydata("mydata.raw", ios::binary);
  
  short value;
  char buf[sizeof(short)];
  int dataCount = 0;
  
  short myDataMat[DATA_DIMENSION][DATA_DIMENSION];
  while (mydata.read(reinterpret_cast&lt;char*&gt;(&amp;buf), sizeof(buf)))
  {
      memcpy(&amp;value, buf, sizeof(value));
      myDataMat[dataCount / DATA_DIMENSION][dataCount%DATA_DIMENSION] = value;
      dataCount++;
  }
  </code></pre>
  
  <p>I like to know how I can detect the endianness in the <code>mydata.raw</code>, and whether endianness affects this program anyway.</p>
  
  <p>Additional Information:</p>
  
  <ul>
  <li>I am only manipulating the data in <code>myDataMat</code> using mathematical operations, and no pointer operation or bitwise operation is done on the data). </li>
  <li>My machine (host) is little endian.</li>
  </ul>