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 a problem feeding the bytes to od
. This is due to the fact that od
always assumes that the endianess matches the endianess of the arch that you are currently on, and I'm on a little endian machine.
I'm looking for an easy one-liner to reverse the byte order. Let's say that you know that the last 8 bytes of a file is a long written by the aforementioned writeLong(...)
method. My current best attempt to print this long is
tail -c 8 file | tac | od -t d8
, but tac
only seems to work on text (fair enough). I've found some references to dd conv=swab
, but this only swaps bytes in pairs, and cannot reverse these eight bytes.
Does anyone know a good one-liner for this?
You could use objcopy:
where num is either 2 or 4.
BASH:
To be a bit more robust depending on the output style of
od
, it may need to compress spaces ( insert"| sed 's/ */ /g'"
after thew8
).Used dd, Luke!
I came up with this Perl one-liner to convert 4-byte integers from one endianness to another:
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:Resorted to Perl in the end. Used a one-liner which I found at PERL One Liners:
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:
Note the next version of GNU coreutils (>= 8.23) will add the --endian={little,big} option to the od command