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

Convert Binary Data Stored as ASCII to Binary

I've got a load of binary data stored as ASCII text in a file like this:
0b 0000 0001
0b 0000 0000
0b 1111 0001
etc


How would I convert this to a raw binary bitstream, and save it to disk?

Can this be done with standard unix tools?
by

1 Answer

Kajalsi45d
Have not fully fleshed this out but try the following:
$ cat demo.sh
#!/bin/bash

while read bb nibble1 nibble2
do
byte=$(printf "%s%s" ${nibble1} ${nibble2} )
hexstr=$(printf '\\\\x%02x' $((2#${byte})) )
eval printf "$hexstr"
done < infile > outfile

Using your sample input:

$ ./demo.sh

$ xxd outfile
00000000: 0100 f1 ...
$

Login / Signup to Answer the Question.