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
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:
You can convert it to binary with:
And then get useful output with something like:
In addition of
xxd
, you should also look at the packages/commandsod
andhexdump
. All are similar, however each provide slightly different options that will allow you to tailor the output to your desired needs. For examplehexdump -C
is the traditional hexdump with associated ASCII translation along side.If you have long text or text in file you can also use the binmake tool that allows you to describe in text format some binary data and generate a binary file (or output to stdout). It allows to change the endianess and number formats and accepts comments.
Its default format is hexadecimal but not limited to this.
First get and compile binmake:
You can pipe it using
stdin
andstdout
:Or use files. So create your text file
file.txt
:Generate your binary file
file.bin
: