Linux Conversion – Convert Binary Mode to Text Mode and Reverse

binaryconversionlinuxod

I converted a simple binary file into a text file with:

od –t x1 Check.tar | cut –c8- > Check.txt

Which gives a content similar to:

 64 65 76 2f 6e 75 6c 6c 00 00 00 00 00 00 00 00
 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
 [...]

What is the opposite way — to convert Check.txt to Check.tar as the original file?

Best Answer

od -An -vtx1 Check.tar > Check.txt

You need -v or od will condense sequences of identical bytes.

For the reverse:

LC_ALL=C tr -cd 0-9a-fA-F < Check.txt | xxd -r -p > Check.tar

Or:

perl -ape '$_=pack "(H2)*", @F' Check.txt > Check.tar

If your purpose is to transfer files over a channel that only supports ASCII text, then there are dedicated tools for that like uuencode:

tar cf - myfiles.* | xz | uuencode myfiles.tar.xz | that-channel 

And to recover those files on the other end:

uudecode < file.uu

would recreate myfiles.tar.xz.

Or:

uudecode -o - < file.uu | xz -d | tar xf -

To extract the files.