Is there a oneliner that converts a binary file from little endian to big endian?

30,564

Solution 1

You cannot do this because for such a conversion, you need to know the meaning of the binary content.

If e.g. there is a string inside a binary file it must not be converted and a 4 byte integer may need different treatment than a two byte integer.

In other words, for a byte order conversion, you need a data type description.

Solution 2

You can byteswap with dd. Is that sufficent? If not, please update your question to give an example of an input file and the expected outfile.

echo hello >infile
dd conv=swab <infile >outfile

hex infile
   0000 68 65 6c 6c 6f 0a                                 hello.
hex outfile
   0000 65 68 6c 6c 0a 6f                                 ehll.o

Solution 3

In order to change file endianess, assuming word (32-bit) size, this 1 liner should work for you:

hexdump -v -e '1/4 "%08x"' -e '"\n"' input_file | xxd -r -p > output_file
Share:
30,564

Related videos on Youtube

Fermat's Little Student
Author by

Fermat's Little Student

Updated on September 18, 2022

Comments

  • Fermat's Little Student
    Fermat's Little Student almost 2 years

    and vice versa.

    I am running a RedHat if relevant.

  • Rob
    Rob over 8 years
    I was just to say that. it is not as simple as just swapping every pair of bytes. for example a 4 byte integer 01 02 03 04 in bigendian could look like this in little endian 04 02 03 01 NOT 02 01 04 03 cs.umd.edu/class/sum2003/cmsc311/Notes/Data/endian.html
  • Brian Rasmussen
    Brian Rasmussen over 8 years
    [citation needed] - AFAIK, unless an application explicitly preserves some byte order and not others, then it is usual for bytes or words to be consistently ordered throughout.
  • Kevin
    Kevin over 8 years
    @DennisWilliamson: A UTF-8 or ASCII string does not have endianness. UTF-8 specifies byte order while ASCII is 7 bit and doesn't have multi-byte tokens. A UTF-16 string does have endianness and may or may not have a BOM to mark that endianness. A Unix binary which is intended to interact with Windows or Java might have both types of string, which means you have to distinguish them when converting.
  • Levi Morrison
    Levi Morrison almost 7 years
    I came here looking for a program where you give it the data description and the data and it converts it. Thus far I have not found such a program but that does not mean it cannot be done.
  • user13397706
    user13397706 over 3 years
    Exactly what I needed when exploiting a str format vuln, The bytes came out in hex as 4 char reversed order (endianness).