4

Is there a way to display the binary contents of a file as zeros and ones?

So instead of something like this:

 DE AD BE EF

We could get output like this:

11011110 10101101 10111110 11101111

Is there a way to do this with basic command line programs / switches?

2 Answers 2

8

Why not use xxd? Use it like so: xxd -b file

For example, doing a xxd -b google-chrome-stable_current_i386.deb | head gives:

0000000: 00100001 00111100 01100001 01110010 01100011 01101000  !<arch
0000006: 00111110 00001010 01100100 01100101 01100010 01101001  >.debi
000000c: 01100001 01101110 00101101 01100010 01101001 01101110  an-bin
0000012: 01100001 01110010 01111001 00100000 00100000 00100000  ary   
0000018: 00110001 00110100 00110000 00111001 00110111 00111001  140979
000001e: 00110001 00111000 00110001 00110101 00100000 00100000  1815  
0000024: 00110000 00100000 00100000 00100000 00100000 00100000  0     
000002a: 00110000 00100000 00100000 00100000 00100000 00100000  0     
0000030: 00110001 00110000 00110000 00110110 00110100 00110100  100644
0000036: 00100000 00100000 00110100 00100000 00100000 00100000    4   

If you want, you can use cut to chop off the left-most column and the right-most column by doing xxd -b google-chrome-stable_current_i386.deb | head | cut -b10-62:

00100001 00111100 01100001 01110010 01100011 01101000 
00111110 00001010 01100100 01100101 01100010 01101001 
01100001 01101110 00101101 01100010 01101001 01101110 
01100001 01110010 01111001 00100000 00100000 00100000 
00110001 00110100 00110000 00111001 00110111 00111001 
00110001 00111000 00110001 00110101 00100000 00100000 
00110000 00100000 00100000 00100000 00100000 00100000 
00110000 00100000 00100000 00100000 00100000 00100000 
00110001 00110000 00110000 00110110 00110100 00110100 
0
1

In case we have hex dump and based on example given in question possible solution looks like following pipe:

echo DE AD BE EF | xxd -r -p | xxd -b

where

echo DE AD BE EF input the example data (white spaces can be preserved)

xxd -r -p converts hex dump to raw bytes

xxd -b converts raw bytes to binary dump

Output:

00000000: 11011110 10101101 10111110 11101111 ....

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .