Is there a utility like hexdump that will handle non-native endian-ness?

6,466

Solution 1

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

Solution 2

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

Solution 3

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.

Solution 4

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

Share:
6,466

Related videos on Youtube

EHN
Author by

EHN

Updated on September 18, 2022

Comments

  • EHN
    EHN almost 2 years

    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?

    • Evan Carroll
      Evan Carroll over 6 years
      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.
  • EHN
    EHN almost 13 years
    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
    EHN over 6 years
    od doesn't have an --endian option on any of the systems I use (GNU coreutils 8.22 or OS X 10.11.6)
  • Evan Carroll
    Evan Carroll over 6 years
    @EHN It was committed in 8.23 which was in 2014. Updated question to reflect that.
  • Darren
    Darren over 6 years
    Welcome to the site. Could you flesh this answer out with an explanation of what this command does and some more detail?