Click here to Skip to main content
15,121,564 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Posted
Comments
ridoy 13-Aug-13 7:26am
   
good decision!
ridoy 13-Aug-13 7:29am
   
Check it http://www.linuxquestions.org/questions/programming-9/converting-a-binary-file-from-little-endian-to-big-endian-899742/

Endianness refers to BYTE ordering within data types, hence your question is vague.
If it's some sort of binary file, you need to read every structure in and reverse the BYTES in it, then serialize back to disk. If it's some sort of multibyte text file you need to lookup the encoding and act accordingly.

sidenote: If you want to swap pairs of bytes in a file use dd with
conv=swab

LE: try this for just reversing everything:

C++
#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;
}


compile and use like this:

./application inputfile outputfile
   
v3
Comments
GiZmoDragonBack 13-Aug-13 7:47am
   
Thanks. I know the essence behind the endianess. I just want to swap all the bytes inside the file. My program is expecting a Big endian file, but I produce a little endian file. The dd is quite close, but it swap in pairs
GiZmoDragonBack 13-Aug-13 10:13am
   
Close enough. I edited your code in the shape the I actually need it. Thanks
@slack7219723

This is the solution that I was looking for. I modify slightly your code to obtain this

C#
#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;
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900