Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

XXD - Usage




Convert binary


Binary to hex dump

xxd <binary_file> > <hexdump_file>


ex: xxd test.bin test.txt

00000000: 9751 8000 9381 81e5 9702 0000 9382 c208  .Q..............
00000010: 7390 5230 7350 4030 d1ad 0000 0000 0000  s.R0sP@0........


Binary to binary digit dump

xxd -b <binary_file> > <binary_digit_dump_file>


ex: xxd -b test.bin test.hex

00000000: 10010111 01010001 10000000 00000000 10010011 10000001  .Q....
00000006: 10000001 11100101 10010111 00000010 00000000 00000000  ......


For more readable, you can use -c option to set the number of bytes per line.

xxd -c <number_of_bytes> <binary_file> > <hexdump_file>

ex: xxd -c 4 test.bin test.txt

00000000: 10010111 01010001 10000000 00000000  .Q..
00000004: 10010011 10000001 10000001 11100101  ....
00000008: 10010111 00000010 00000000 00000000  ....



Revert to binary


Hexdump to binary

xxd -r <hexdump_file> > <binary_file>

ex: xxd -r test.txt test.bin




Modify hexdump


1. Convery binary to hexdump

xxd <binary_file> > <hexdump_file>

ex: <hexdump_file>

00000000: 9751 8000 9381 81e5 9702 0000 9382 c208  .Q..............
00000010: 7390 5230 7350 4030 d1ad 0000 0000 0000  s.R0sP@0........

2. Modify hexdump

Modify the hexdump file using any text editor or command line. Able to leave the ASCII part as unchanged even modified the hex part.

ex: sed -i 's/9751/1234/g' <hexdump_file>

00000000: 1234 8000 9381 81e5 9702 0000 9382 c208  .Q..............
00000010: 7390 5230 7350 4030 d1ad 0000 0000 0000  s.R0sP@0........

3. Convert back to binary

xxd -r <hexdump_file> > <binary_file>



Cherry-pick binary

For some binary file, we may drop some part of the binary file and keep the rest.

1. Convery binary to hexdump

xxd <binary_file> > <hexdump_file>

ex: <hexdump_file>

00000000: 9751 8000 9381 81e5 9702 0000 9382 c208  .Q..............
00000010: 7390 5230 7350 4030 d1ad 0000 0000 0000  s.R0sP@0........
00000020: 0000 0000 0000 0000 0000 0000 0000 0000  ................

2. Remove the part you want to drop

ex: sed -i '1d' <hexdump_file>

Remove second line from the hexdump file with 16 bytes or offset 0x00.

00000010: 7390 5230 7350 4030 d1ad 0000 0000 0000  s.R0sP@0........
00000020: 0000 0000 0000 0000 0000 0000 0000 0000  ................

3. Convert back to binary

Since we removed 16 bytes from the hexdump file, the binary file expected will be 16 bytes shorter.

  • if we revert the hexdump file to binary directly, we will found the removed second line is padding with 0x00 on offset 0x10.

    ex: xxd -r <hexdump_file> > <binary_file> && xxd <binary_file> > <hexdump_file>

    00000000: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00000010: 7390 5230 7350 4030 d1ad 0000 0000 0000  s.R0sP@0........
    00000020: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    

Suppress leading bytes of offset 0x10 to 0x00

xxd -r -s -<suppress_bytes> <hexdump_file> > <binary_file>

ex: xxd -r -s -0x10 test.txt test.bin

00000000: 7390 5230 7350 4030 d1ad 0000 0000 0000  s.R0sP@0........
00000010: 0000 0000 0000 0000 0000 0000 0000 0000  ................