and vice versa.

I am running a RedHat if relevant.

  • 6
    What type of binary file? – Stéphane Chazelas Oct 29 '15 at 16:47
  • 2
    In any case, for any file format, I'm sure you could write a one-liner to convert its endianness using Perl and pack / unpack. For some formats, it'll just be a longer line than for others. ;-) – Ilmari Karonen Oct 29 '15 at 21:13

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.

  • 7
    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 – Rob Oct 29 '15 at 16:36
  • 1
    Agreed, endianness makes no sense on a raw, typeless byte stream... this should be the accepted answer if OP does not edit his question – Thomas Oct 30 '15 at 11:02
  • [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. – Dennis Williamson Oct 30 '15 at 15:52
  • 1
    @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. – Kevin Oct 30 '15 at 18:42
  • 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. – Levi Morrison Aug 10 '17 at 16:37

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

If you don't care about file contents and just want to swap bytes, then try endconv. It is just a wrapper around standard byte conversion functions, so it supports conversion by 2, 4 and 8 byte long integers. It's not one liner though because it is separate program.


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
  • Exactly what I needed when exploiting a str format vuln, The bytes came out in hex as 4 char reversed order (endianness). – Marshall Whittaker Mar 20 at 2:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.