0

I wrote a c program to read a MP3 file and print the TAG2 fields. The source code is:

void main(void)
{
   FILE *w;
   char c[10]={0};    
   int ver, flag, size;    
   w=fopen("test.mp3,"rb");   
   fread(c,1,3,w);    
   printf("TAG2 identifier:%s\n",c);
   fread(&ver,1,2,w);    
   printf("TAG2 version:%d\n",ver);    
   fread(&flag,1,1,w);    
   printf("Flags:%d\n",flag);    
   fread(&size,1,4,w);  //????????    
   ..........
}

I know that the most significant bit in each byte of size is set to 0 and should be ignored.
But it seems that when read() reads the 4 byte of size, the byte order is reversed. How can I read the size in correct byte order?

Sumit Singh
  • 15,743
  • 6
  • 59
  • 89
katy
  • 1
  • 2

1 Answers1

0

The .mp3 file format specification should describe whether numbers are stored least significant byte first (AKA little endian) or most significant byte first (AKA big endian).

Using that knowledge you should be able to reconstruct multi-byte integers from individual bytes using a combination of operators (* (or <<), + (or |)) and appropriate casting. I did .wav file saving in a similar fashion in this answer, using the reverse of the approach (with / and %).

Community
  • 1
  • 1
Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180