Hexdump reverse command

前端 未结 4 578
悲哀的现实
悲哀的现实 2021-02-08 14:54

The hexdump command converts any file to hex values.

But what if I have hex values and I want to reverse the process, is this possible?

相关标签:
4条回答
  • 2021-02-08 15:18

    I've written a short AWK script which reverses hexdump -C output back to the original data. Use like this:

    reverse-hexdump.sh hex.txt > data
    

    Handles '*' repeat markers and generating original data even if binary. hexdump -C and reverse-hexdump.sh make a data round-trip pair. It is available here:

    • GitHub reverse-hexdump repo
    • Direct to reverse-hexdump.sh
    0 讨论(0)
  • 2021-02-08 15:22

    There is a similar tool called xxd. If you run xxd with just a file name it dumps the data in a fairly standard hex dump format:

    # xxd bdata
    0000000: 0001 0203 0405
    ......
    

    Now if you pipe the output back to xxd with the -r option and redirect that to a new file, you can convert the hex dump back to binary:

    # xxd bdata | xxd -r >bdata2
    # cmp bdata bdata2
    # xxd bdata2
    0000000: 0001 0203 0405
    
    0 讨论(0)
  • 2021-02-08 15:30

    If you have only a hexdump generated with hexdump you can use the following command

    sed -E 's/ /: /;s/ (..)(..)/ \2\1/g;$d' dump | xxd -r
    

    The sed part converts hexdump's format into xxd's format, at least so far that xxd -r works.

    Known Bugs (see comment section)

    • A trailing null byte is added if the original file was of odd length (e.g. 1, 3, 5, 7, ..., byte long).
    • Repeating sections of the original file are not restored correctly if they were hexdumped using a *.
    0 讨论(0)
  • 2021-02-08 15:30

    There is a tonne of more elegant ways to get this done, but I've quickly hacked something together that Works for Me (tm) when regenerating a binary file from a hex dump generated by hexdump -C some_file.bin:

    sed 's/\(.\{8\}\)  \(..\) \(..\) \(..\) \(..\) \(..\) \(..\) \(..\) \(..\)/\1: \2\3 \4\5 \6\7 \8\9/g' some_file.hexdump | sed 's/\(.*\)  \(..\) \(..\) \(..\) \(..\) \(..\) \(..\) \(..\) \(..\)  |/\1 \2\3 \4\5 \6\7 \8\9  /g' | sed 's/.$//g' | xxd -r > some_file.restored
    

    Basically, uses 2 sed processeses, each handling it's part of each line. Ugly, but someone might find it useful.

    0 讨论(0)
提交回复
热议问题