本文介绍了将文件从Little endian转换为Big endian的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在Linux 32位下将文件从Little Endian转换为Big Endian。我知道我可以自己做这个程序,但我想知道是否已经完成了任何可以完成工作的程序。
提前谢谢
Hi,
I need to convert a file from Little Endian to Big Endian under Linux 32 bit. I know that I can do the program myself, but I was wondering if there is any program already done that can do the job.
Thanks in advance
推荐答案
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
FILE* input = NULL;
FILE* output = NULL;
unsigned long size = 0;
unsigned long itr = 0;
unsigned char* buffer;
if(argc != 3)
return 0;
input = fopen(argv[1],"rb");
output = fopen(argv[2],"wb");
if(!input || !output)
fprintf(stderr,"could not open file");
fseek(input,0,SEEK_END);
size = ftell(input);
fseek(input,0,SEEK_SET);
buffer = (unsigned char*)malloc(size);
fread(buffer,1,size,input);
fclose(input);
for(itr = size;itr>0;itr--)
{
fwrite(&buffer[itr-1],1,1,output);
}
fclose(output);
return 0;
}
编译和使用如下:
./application inputfile outputfile
compile and use like this:
./application inputfile outputfile
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
FILE* input = NULL;
FILE* output = NULL;
unsigned long size = 0;
unsigned long itr = 0;
unsigned char* buffer;
if(argc != 3)
return 0;
input = fopen(argv[1],"rb");
output = fopen(argv[2],"wb");
if(!input || !output)
fprintf(stderr,"could not open file");
fseek(input,0,SEEK_END);
size = ftell(input);
fseek(input,0,SEEK_SET);
buffer = (unsigned char*)malloc(size);
fread(buffer,1,size,input);
fclose(input);
for(itr = 0;itr<size;itr=itr+4)
{
fwrite(&buffer[itr + 3],1,1,output);
fwrite(&buffer[itr + 2],1,1,output);
fwrite(&buffer[itr + 1],1,1,output);
fwrite(&buffer[itr + 0],1,1,output);
}
fclose(output);
return 0;
}
这篇关于将文件从Little endian转换为Big endian的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!