7

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?

1
  • this is probably not on topic at super-user and should instead be on Unix & Linux please review my answer though for a better method than the one chosen. – Evan Carroll Nov 13 '17 at 22:10
3

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
1
  • I'm not really happy with relying on perl and having to do the (admittedly very limited) extra typing compared to hexdump, but have become convinced better options don't exist. – EHN Jul 13 '11 at 14:38
5

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
2

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.

1

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

2
  • 1
    od doesn't have an --endian option on any of the systems I use (GNU coreutils 8.22 or OS X 10.11.6) – EHN Nov 15 '17 at 22:00
  • 1
    @EHN It was committed in 8.23 which was in 2014. Updated question to reflect that. – Evan Carroll Nov 15 '17 at 22:36
0

dd conv=swab < file.dat | hexdump

1
  • 3
    Welcome to the site. Could you flesh this answer out with an explanation of what this command does and some more detail? – Darren Jan 12 '18 at 14:41

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.