在C程序中读取mp3文件的TAG2 [英] Reading TAG2 of a mp3 file in a c program

查看:60
本文介绍了在C程序中读取mp3文件的TAG2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个 c 程序来读取MP3文件并打印TAG2字段.源代码是:

  void main(void){文件* w;字符c [10] = {0};int ver,标志,大小;w = fopen("test.mp3," rb);fread(c,1,3,w);printf("TAG2标识符:%s \ n",c);fread(& ver,1,2,w);printf("TAG2版本:%d \ n",ver);fread(& flag,1,1,w);printf("Flags:%d \ n",flag);fread(& size,1,4,w);//?????????.....} 

我知道大小的每个字节中的最高有效位都设置为0,应该忽略.
但是似乎当 read()读取4个字节的大小时,字节顺序相反.如何以正确的字节顺序读取大小?

解决方案

.mp3文件格式规范应描述存储的数字是存储的最低有效字节在前(AKA小字节序)还是最高有效字节在前(AKA大字节序)./p>

使用该知识,您应该能够使用运算符(*(或<<),+(或|))和适当的转换来组合从单个字节中重建多字节整数.我使用与方法相反的方法(带有/和%)以类似方式在此答案中保存了.wav文件.

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?

解决方案

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 %).

这篇关于在C程序中读取mp3文件的TAG2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆