Problem :
Hexdump’s ability to read binary data and format it appropriately so it can, for example, be piped to awk is very useful, but I regularly need to read files in which the binary data is of a different endian-ness from that native to the system. In particular, I need to read big-endian data on a little endian machine. My ideal solution would be “hexdump” with a switch to reverse the endian-ness, but such a switch doesn’t seem to exist.
Are there any good “next-best” solutions to this problem?
Solution :
Is there a utility like hexdump that will handle non-native endian-ness?
Yes, the utility is called Perl.
Well actually Data::HexDumper – though you could roll your own.
number_format A string specifying how to format the data. It can be any of the following, which you will notice have the same meanings as they do to perl's pack function: C - unsigned char S - unsigned 16-bit, native endianness v or S< - unsigned 16-bit, little-endian n or S> - unsigned 16-bit, big-endian L - unsigned 32-bit, native endianness V or L< - unsigned 32-bit, little-endian N or L> - unsigned 32-bit, big-endian Q - unsigned 64-bit, native endianness Q< - unsigned 64-bit, little-endian Q> - unsigned 64-bit, big-endian
At least for 16-bit words one can pipe it through dd conv=swab
as in,
cat file.dat | dd conv=swab | od -t x2
As pixelbeat suggests, you could use objcopy:
$ objcopy -I binary -O binary --reverse-bytes=num inputfile.bin outputfile.bin
where num
is 2 for 16 bit words, 4 for 32 bit words and 8 for 64 bit words.
Unfortunately objcopy has no option to accept input from stdin
or write output to stdout
, so in order to use it as a pipe you would need to write a wrapper script that creates temporary files.
This answer is copied from https://stackoverflow.com/a/19288235/1979048 and from https://serverfault.com/a/329207/157443.
Just use od
(8.23 or greater). It’s part of the Linux Standard Base, and in my opinion it’s better than hexdump
at everything. It provides an endian
option,
‘--endian=order’
Reorder input bytes, to handle inputs with differing byte orders, or to provide consistent output independent of the endian convention of the current system. Swapping is performed according to the specified --type size and endian order, which can be ‘little’ or ‘big’.
You can use it like this,
od --endian big -x
The patch to add this was commited in 2014