Here's some sed to coax dc to translate od's output to base 2:
od -t d1z -w4 -v -N12 </dev/urandom |
sed -e '1i2o' -e 's/.*/[&]p/p;$d
s/>/]n [>/;s/[^ ]*/&]n [/;h;s/>.*//;
s/ -/ _/g;s/ [^] [][^ ]*/ ]n&n [ /g;G
s/\n[^>]*//' |
dc
It's a little simpler now - which is not to mention faster - but it's still no beauty queen. It also prints the decimal and base 2 values of all the bytes.
When I run it I get:
0000000 -43 125 -117 -39 >.}..<
0000000 -101011 1111101 -1110101 -100111 >.}..<
0000004 62 28 80 61 >>.P=<
0000004 111110 11100 1010000 111101 >>.P=<
0000010 6 14 120 -16 >..x.<
0000010 110 1110 1111000 -10000 >..x.<
0000014
Or...
echo aBcD | od -t d1z -w4 -v | sed ... | dc
0000000 97 66 99 68 >aBcD<
0000000 1100001 1000010 1100011 1000100 >aBcD<
0000004 10 >.<
0000004 1010 >.<
0000005
The field widths could use a little work, but it's all yours. You don't need the -N12 option - I just used that so I didn't choke on an endless pipe of pseudo-random data. And the -w4 specifies 4 bytes per line but you should be able to use any number of bytes. Also the 1i2o sed command is a dc instruction regarding its output base - 2 for binary - but any base between 2 and 16 should work just as well. If wish to see, for instance, hexadecimal and base 2 output you'll need to add '16i' to that first sed statement and change od's -t d1z option to t x1z.
Other options include...
printf does this:
printf '%o\n%x\n' 128 128
200
80
...even...
printf '%o\n%x\n' "'a" "'a"
141
61
Binary isn't quite as simple, but bc can do all of it if you set its obase= to your specifications:
printf 'obase=2;%d
obase=8;%d
obase=16;%d
obase=2;%d
' 64 128 "'A" "'a" |
bc
OUTPUT
1000000
200
41
1100001
dc isn't quite as chatty:
printf '%do%dn10P' 2 64 8 128 16 "'A" 2 "'a" |dc
OUTPUT
1000000
200
41
1100001
Do man dc bc for more info.
And then again, for file streams you can always use od:
for o in o d x ; do
echo aBcD |
od -A n -t ${o}1z -v -w4
done
OUTPUT
141 102 143 104 >aBcD<
012 >.<
97 66 99 68 >aBcD<
10 >.<
61 42 63 44 >aBcD<
0a >.<
With ^that^ one I tell od not to print offsets - which I'm now second-guessing - that I want outputs of -type o, d, or x one byte at a time and that I want the ASCII representationz of each byte appended to the end of the line, -verbosely (so it doesn't just print me an 0* for 0000) at -w4 bytes per line.
Without -A n it prints:
0000000 141 102 143 104 >aBcD<
0000004 012 >.<
0000005
0000000 97 66 99 68 >aBcD<
0000004 10 >.<
0000005
0000000 61 42 63 44 >aBcD<
0000004 0a >.<
0000005
And any combination of dc bc od is of course possible in a |pipeline.