Hexdump Reverse Command

Hexdump reverse command

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

Command to convert hexdump back to text

Use xxd with the revert option -r

xxd -p -r file.txt

gives a result of

<?php

function user_exists($un) {
return true;
}

?>

hexdump output order

This is because hexdump defaults to using 16-bit words and you are running on a little-endian architecture. The byte sequence b1 c3 is thus interpreted as the hex word c3b1. The -C option forces hexdump to work with bytes instead of words.

How to print only the hex values from hexdump without the line numbers or the ASCII table?

You can specify the exact format that you want hexdump to use for output, but it's a bit tricky. Here's the default output, minus the file offsets:

hexdump -e '16/1 "%02x " "\n"' file.bin

(To me, it looks like this would produce an extra trailing space at the end
of each line, but for some reason it doesn't.)

How to convert hexadecimal hash into binary at terminal?

Types of Hexdumps

The problem is that xxd expects a formatted hexdump, but you are sending in a string of hex numbers. This is what xxd refers to as a "plain hexdump".

A formatted hexdump looks like this (quote by Joseph Campbell):

$ printf "Computers are like old testament gods; lots of rules and no mercy." | xxd
00000000: 436f 6d70 7574 6572 7320 6172 6520 6c69 Computers are li
00000010: 6b65 206f 6c64 2074 6573 7461 6d65 6e74 ke old testament
00000020: 2067 6f64 733b 206c 6f74 7320 6f66 2072 gods; lots of r
00000030: 756c 6573 2061 6e64 206e 6f20 6d65 7263 ules and no merc
00000040: 792e y.

xxd will, by default, create this kind of hexdump.

Use -p for hex strings

Per the xxd manpage, -r expects to read a formatted hexdump:

-r | -revert
reverse operation: ...
Use the combination -r -p to read plain hexadecimal
dumps without line number information and without a particular column layout.
...

To reiterate, by default, xxd -r expects a formatted hexdump. To read or write a "plain hexdump" with xxd, use the -p option. Note that xxd expects flags to come before arguments, so if you end with a flag, you will get unexpected behavior.

Verification

If you can see that running openssl twice (initially with -binary) and running openssl with xxd produce the same result.

$ openssl dgst -sha256 <( printf "Hello World!" ) |  awk '{print $2}' > myHashHex; 
$ xxd -r -p myHashHex | openssl dgst -sha256
(stdin)= 61f417374f4400b47dcae1a8f402d4f4dacf455a0442a06aa455a447b0d4e170
$ openssl dgst -sha256 -binary <( printf "Hello World!" ) | openssl dgst -sha256
(stdin)= 61f417374f4400b47dcae1a8f402d4f4dacf455a0442a06aa455a447b0d4e170


Related Topics



Leave a reply



Submit