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?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityHave 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 ...
$
xxd -c 1 -b | sed 's/^[0-9a-f]*:/0b/' | cut -f 1-2 -d\
but xxd doesn't support-b
with-r
:/