20

and vice versa.

I am running a RedHat if relevant.

2
  • 6
    What type of binary file? Commented Oct 29, 2015 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. ;-) Commented Oct 29, 2015 at 21:13

4 Answers 4

38

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
35

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.

5
  • 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
    Commented Oct 29, 2015 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
    Commented Oct 30, 2015 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. Commented Oct 30, 2015 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
    Commented Oct 30, 2015 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. Commented Aug 10, 2017 at 16:37
8

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

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .