How can some binary expressed in ASCII 0s and 1s be converted to a binary file?

I've got a PDF expressed as an ASCII file of 0s and 1s, produced in the following way:

filepath="Manna.pdf"
data="$((echo obase=2; hexdump -ve'/1 "%u\n"' "${filepath}") | bc | xargs printf %08i)"
inputText="$(echo "${inputText}" | sed 's/\(.*\)/\L\1/')"
echo "${data}" > Manna.txt

How can this be converted back to PDF?

2

1 Answer

I don't know why you'd want to do that, but perhaps you could use Perl's oct to convert each 8-bit binary substring into its numeric value and print that as a char:

perl -pe 's/([01]{8})/sprintf "%c", oct("0b$1")/ge'

Ex.

$ printf 'foo bar\nbaz\n' | { echo obase=2; hexdump -ve'/1 "%u\n"' ; } | bc | xargs printf %08i | perl -pe 's/([01]{8})/sprintf "%c", oct("0b$1")/ge'
foo bar
baz
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like