Command-line to reverse byte order/change endianess

后端 未结 6 1233
逝去的感伤
逝去的感伤 2021-02-07 18:49

I\'m hacking around in some scripts trying to parse some data written by Javas DataOutputStream#writeLong(...). Since java always seems to write big endian, I have

相关标签:
6条回答
  • 2021-02-07 19:03

    Resorted to Perl in the end. Used a one-liner which I found at PERL One Liners:

    tail -c 8 file | perl -0777e 'print scalar reverse <>' | od -t d8
    

    The 0777 separator char was a bit puzzling to me, but this page at debian admin seems to suggest that it is a placeholder for 'no record separator', triggering a complete reverse byte-per byte.

    Other suggestions are welcome.

    EDIT: Found another command in a comment to tac.c, which I downloaded from GNU coreutils:

    Copy each FILE, or the standard input if none are given or when a FILE name of "-" is encountered, to the standard output with the order of the records reversed. The records are separated by instances of a string, or a newline if none is given. By default, the separator string is attached to the end of the record that it follows in the file.

    Options: -b, --before The separator is attached to the beginning of the record that it precedes in the file. -r, --regex The separator is a regular expression. -s, --separator=separator Use SEPARATOR as the record separator.

    To reverse a file byte by byte, use (in bash, ksh, or sh): tac -r -s '.\| ' file

    0 讨论(0)
  • 2021-02-07 19:08

    Used dd, Luke!

    dd if=sourcefile of=resultfile conv=swab
    
    0 讨论(0)
  • 2021-02-07 19:12

    I came up with this Perl one-liner to convert 4-byte integers from one endianness to another:

    $ perl -e 'open F,shift; do { read(F,$a,4); print scalar reverse($a);} while(!eof(F));' bigend.bin > littlend.bin
    

    That probably works fine on real Linux machines, but Cygwin bit me in the end, treating the binary file as text and inserting a 0x0D (aka CR) before each 0x0A byte (aka newline). But if you pipe to cat -, it seems to leave it alone. This works for me:

    $ perl -e 'open F,shift; do { read(F,$a,4); print scalar reverse($a);} while(!eof(F));' bigend.bin | cat - > littlend.bin
    
    0 讨论(0)
  • 2021-02-07 19:14

    You could use objcopy:

    $ objcopy -I binary -O binary --reverse-bytes=num inputfile.bin outputfile.bin
    

    where num is either 2 or 4.

    0 讨论(0)
  • 2021-02-07 19:23

    BASH:

    od -b -v -w8 | while read pfx b8 ; do [ "$b8" ] && echo -n 12345678 | tr 87654321 \\${b8// /\\} ; done
    

    To be a bit more robust depending on the output style of od, it may need to compress spaces ( insert "| sed 's/ */ /g'" after the w8).

    0 讨论(0)
  • 2021-02-07 19:27

    Note the next version of GNU coreutils (>= 8.23) will add the --endian={little,big} option to the od command

    0 讨论(0)
提交回复
热议问题