#include<stdio.h>#include<stdlib.h>char*trans(unsignedchar ch);//用这个函数进行二进制到十六进制的转换intmain(void){
FILE *fp;unsignedchar*charPoint;if((charPoint =(unsignedchar*)calloc(210406885,1))==NULL){printf("Not able to allocate memory.\n");exit(0);}//文件打开 if((fp =fopen("画江湖之灵主21集.mp4","rb"))==NULL){printf("File open error!\n");exit(0);}fread(charPoint,1,10000, fp);// 视频文件过大,这里先只读取前10000个字节for(int i =0; i <10000; i++){printf("%s ",trans(*(charPoint+i)));}fclose(fp);free(charPoint);return0;}char*trans(unsignedchar ch){staticchar chs[3];//static变量的生命周期更长,当该函数执行完毕后,内存不会被立即释放,这样就可以用指针将它的内存地址返回给主函数使用。switch(ch){//这段switch语句由python程序生成的case0:
chs[0]='0';
chs[1]='0';
chs[2]=0;break;case1:
chs[0]='0';
chs[1]='1';
chs[2]=0;break;case2:
chs[0]='0';
chs[1]='2';
chs[2]=0;break;/****
中间内容过长,省略
*********/case254:
chs[0]='f';
chs[1]='e';
chs[2]=0;break;case255:
chs[0]='f';
chs[1]='f';
chs[2]=0;break;}return chs;}
运行结果为如下:(将结果输出到屏幕是一个比较慢的过程,如果将输出结果写入一个文件的话会快很多)
然后我们使用Hex Editor Neo打开该视频文件来验证输出结果的正确性:
通过对比我们就可以看出输出的结果是完全正确的。
下面再给出一个将输出结果写入文件的代码:
#include<stdio.h>#include<stdlib.h>char*trans(unsignedchar ch);//由于这个函数的函数体太长了,这里省略,其函数体的代码和上面的一样intmain(void){
FILE *fp;
FILE *f;unsignedchar*charPoint;if((charPoint =(unsignedchar*)calloc(210406885,1))==NULL){printf("Not able to allocate memory.\n");exit(0);}//文件打开 if((fp =fopen("画江湖之灵主21集.mp4","rb"))==NULL){printf("File open error!\n");exit(0);}//将结果输出到hex.txt文件if((f =fopen("hex.txt","a"))==NULL){printf("hex.txt open error!\n");exit(0);}fread(charPoint,1,210406880, fp);for(int i =0; i <210406880; i++){fprintf(f,"%s ",trans(*(charPoint+i)));// 文件格式化写入}fclose(f);fclose(fp);free(charPoint);return0;}