title: BOM endian in C
body: <p>I understand that Big Endian and Little Endian can be encoded with a BOM that gives them away, but i'm so confused as for how i can evaluate this in C given a file. </p>
  
  <pre><code>00 00 FE FF -&gt; UTF-32, big-endian
  FF FE 00 00 -&gt; UTF-32, little-endian
  FE FF -&gt; UTF-16, big-endian
  FF FE -&gt; UTF-16, little-endian
  </code></pre>
  
  <p>I have this piece of code to get the bytes from the file, but how do i know if it's little or big endian UTF-16 assuming the file starts with a BOM \xFF\xFE or \xFE\xFF.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main(int argc, char *argv[]){
  unsigned char c;
  
  FILE *f = fopen(argv[1], "r");
  
  while (fread(&amp;c, sizeof(char), 1, f) == 1){
   fprintf(stdout, "%x\n", c);
  }
  }
  </code></pre>
  
  <p>And what would the file containing this BOM look like? (In either byte or regular text)?
  I hope someone can help. Thanks.</p>
  
  <p>I'm confused as for how to read the file and test if the first byte or bytes containing the BOM are little endian or big endian? How do i do that?</p>