Convert binary mode to text mode and the reverse option
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.
Answering the X part of this XY problem, I would recommend you investigate the reason your binary file transfers don't transfer properly.
If it turns out the reason is because you don't have an 8-bit clean datapath you could then use existing tools that were created to handle this situation, such as base64
or even uuencode
. Old but still very effective.
tar czvf - /etc/h* | base64 >/tmp/tar.tgz.b64
ls -l /tmp/tar.tgz.b64
-rw-r--r-- 1 root root 7364 May 26 11:52 /tmp/tar.tgz.b64
...
base64 -d /tmp/tar.tgz.b64 | tar tzvf -
or
tar czvf - /etc/h* | uuencode - >/tmp/tar.tgz.uue
ls -l /tmp/tar.tgz.uue
-rw-r--r-- 1 root root 7530 May 26 11:51 /tmp/tar.tgz.uue
...
uudecode /tmp/tar.tgz.uue | tar xzvf -
In my case I didn't have xxd or uudecode on the remote device but I did have bash. I ended up with the following:
Convert from binary to txt with:
od -An -vtx1 myfile.bin > myfile.txt
Then convert back from txt to binary with:
while read p; do
IFS=' ' read -r -a array <<< "$p"
for index in "${!array[@]}"
do
echo -en "\x${array[index]}"
done
done < myfile.txt > myfile.bin