How to convert a text file to binary file using linux commands

74,608

use xxd -r. it reverts a hexdump to its binary representation.

source and source

Edit: The -p parameter is also very useful. It accepts "plain" hexadecimal values, but ignores whitespace and line changes.

So, if you have a plain text dump like this:

echo "0000 4865 6c6c 6f20 776f 726c 6421 0000" > text_dump

You can convert it to binary with:

xxd -r -p text_dump > binary_dump

And then get useful output with something like:

xxd binary_dump
Share:
74,608
aMa
Author by

aMa

Updated on July 09, 2022

Comments

  • aMa
    aMa almost 2 years

    I have hex code of a binary in text (string) format. How do I convert it to a binary file using linux commands like cat and echo ?

    I know command following command with create a binary test.bin. But what if this hexcode is in another .txt file ? How do I "cat" the content of text file to "echo" and generate a binary file ?

    # echo -e "\x00\x001" > test.bin

  • szotsaki
    szotsaki over 4 years
    In a one liner: xxd -r -p <(echo "0A 04 00 00 34") | xxd
  • Jacqueline P.
    Jacqueline P. over 3 years
    does not work on my linux guess need to install dependencies
  • daouzli
    daouzli over 3 years
    @JacquelineP. what dependencies ? It is a C++11 open source program so it depends of course on a compiler as g++ but there is no exotic library. Only standard libraries are used (as I remember regex is std).
  • Elly Ambet
    Elly Ambet about 3 years
    And how do I reverse the above action? as in: xxd -r -p text_dump > binary_dump gives me a file(binary_dump). How do I revert the action of converting the binary_dump to text_dump??
  • 1010
    1010 about 3 years
    @EllyAmbet you can use xxd -p binary_dump > text