Shell: How to read the bytes of a binary file and print as hexadecimal?

  • pdo

    pdo Correct answer

    10 years ago

    Use hexdump(1)

    $ hexdump -x /usr/bin/hexdump 
    0000000    feca    beba    0000    0300    0001    0700    0080    0300
    0000010    0000    0010    0000    5080    0000    0c00    0000    0700
    0000020    0000    0300    0000    00a0    0000    b06f    0000    0c00
    0000030    0000    1200    0000    0a00    0100    0010    0000    107c
    0000040    0000    0c00    0000    0000    0000    0000    0000    0000
    0000050    0000    0000    0000    0000    0000    0000    0000    0000
    

    ...

    Also check out `od`. There is also a vi-style hex editor called `hexer`.

    I like the output of "hexdump -C file" better. xxd is also a nice tool.

    show in bash hexa format `hexdump -e '"\\\x" /1 "%02x"' filename`

    For information, the first column is the hexadecimal offset of the bytes, the rest of the line is 8 sets of two-byte displays, i.e. 16 bytes, which is why the second line starts with an offset of `10`, which is 16 in hexadecimal. The two-byte representation depends on the endianness of the system. Type `man hexdump` for the full details.

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM