Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

ASCII to Binary and Binary to ASCII conversion tools?

Which is a good tool to convert ASCII to binary, and binary to ASCII?

I was hoping for something like:
$ echo --binary "This is a binary message"
01010100 01101000 01101001 01110011 00100000 01101001 01110011 00100000 01100001 00100000 01100010 01101001 01101110 01100001 01110010 01111001 00100000 01101101 01100101 01110011 01110011 01100001 01100111 01100101


Or, more realistic:
$ echo "This is a binary message" | ascii2bin
01010100 01101000 01101001 01110011 00100000 01101001 01110011 00100000 01100001 00100000 01100010 01101001 01101110 01100001 01110010 01111001 00100000 01101101 01100101 01110011 01110011 01100001 01100111 01100101


And also the reverse:
$ echo "01010100 01101000 01101001 01110011 00100000 01101001 01110011 00100000 01100001 00100000 01100010 01101001 01101110 01100001 01110010 01111001 00100000 01101101 01100101 01110011 01110011 01100001 01100111 01100101" | bin2ascii
This is a binary message

PS: I'm using bash

PS2: I hope I didn't get the wrong binary
by

1 Answer

Bharatgxwzm
Using bc and bash:
#!/bin/bash

chrbin() {
echo $(printf \\$(echo "ibase=2; obase=8; $1" | bc))
}

ordbin() {
a=$(printf '%d' "'$1")
b=$(echo "obase=2; $a" | bc)
printf '%08d' $b
}

ascii2bin() {
echo -n $ | while IFS= read -r -n1 char
do
ordbin $char | tr -d '\n'
echo -n " "
done
}

bin2ascii() {
for bin in $

do
chrbin $bin | tr -d '\n'
done
}
ascii2bin "This is a binary message"
bin2ascii 01010100 01101000 01101001 01110011 00100000 01101001 01110011 00100000 01100001 00100000 01100010 01101001 01101110 01100001 01110010 01111001 00100000 01101101 01100101 01110011 01110011 01100001 01100111 01100101

Login / Signup to Answer the Question.