title: Accessing binary MP3 Header in C via fopen
body: <p>I am trying to extract the mp3 header from a file. This is different then the ID3 tags -- the mp3 header is where information about the MPEG version, bit rate, frequency, etc is held.</p>
  
  <p>You can see an overview of the mp3 header structure here: <a href="http://upload.wikimedia.org/wikipedia/commons/0/01/Mp3filestructure.svg" rel="nofollow noreferrer">http://upload.wikimedia.org/wikipedia/commons/0/01/Mp3filestructure.svg</a></p>
  
  <p>My problem is, despite loading the file and now receiving valid (as far as I know) binary output, I am not seeing the expected values. The first 12 bits of the mp3 file should be all ones, for the mp3 sync word. However, I am receiving something different for the first 8 bits alone. This would suggest a problem  to me.</p>
  
  <p>As a side note, I have a valid mp3 file being attached via fopen</p>
  
  <pre><code>// Main function
  int main (void)
  {
      // Declare variables
      FILE *mp3file;
      char requestedFile[255] = "";
      unsigned long fileLength;
  
      // Counters
      int i;
  
      // Tryout
      unsigned char byte; // Read from file
      unsigned char mask = 1; // Bit mask
      unsigned char bits[8];
  
      // Memory allocation with malloc
      // Ignore this at the moment! Will be used in the future
      //mp3syncword=(unsigned int *)malloc(20000);
  
      // Let's get the name of the file thats requested
      strcpy(requestedFile,"testmp3.mp3"); // lets hardcode this into here for now
  
      // Open the file
      mp3file = fopen(requestedFile, "rb"); // open the requested file with mode read, binary
      if (!mp3file){
          printf("Not found!"); // if we can't find the file, notify the user of the problem
      }
  
      // Let's get some header data from the file
      fseek(mp3file,0,SEEK_SET);
      fread(&amp;byte,sizeof(byte),1,mp3file);
  
      // Extract the bits
      for (int i = 0; i &lt; sizeof(bits); i++) {
          bits[i] = (byte &gt;&gt; i) &amp; mask;
      }
  
      // For debug purposes, lets print the received data
      for (int i = 0; i &lt; sizeof(bits); i++) {
          printf("Bit: %d\n",bits[i]);
      }
  </code></pre>