4

How do I dump a "pure" and plain binary sequence of bits?

For example, I have this:

 0000000: 10001001 01010000 01001110 01000111 00001101 00001010  .PNG..
 0000006: 00011010 00001010 00000000 00000000 00000000 00001101  ......
 000000c: 01001001 01001000 01000100 01010010 00000000 00000000  IHDR..
 0000012: 00000010 01011000 00000000 00000000 00000001 10010000  .X....
 0000018: 00001000 00000010 00000000 00000000 00000000 11111101  ......
 000001e: 01010111 10001001 11001111 00000000 00000000 00000000  W.....
 0000024: 00000111 01110100 01001001 01001101 01000101 00000111  .tIME. 

Those line numbers on the left, and the ASCII on the right is a problem for me. I did try this to remove it:

xxd -b /root/Desktop/image.png | sed -r "s/\d32{3,}.*//g" | sed "s/.*://" | sed "s/\d32//g"

However, I did not succeed:

 100010010101000001001110010001110000110100001010.PNG..
 000110100000101000000000000000000000000000001101......
 010010010100100001000100010100100000000000000000IHDR..
 000000100101100000000000000000000000000110010000.X....
 000010000000001000000000000000000000000011111101......
 010101111000100111001111000000000000000000000000W.....
 000001110111010001001001010011010100010100000111.tIME.

Is there a tool like 'xxd' or 'hexdump' or something that could produce a clean binary sequence out of a file?

For example,

 01010101000100100100010100100010000010101010101010101001011
 10101010101010100100101010101010100000010001000100001000000
  • On what operating system? It looks like you're running some kind of *nix but which one? Is it Linux? UNIX? OSX? Something else? – terdon Mar 29 '15 at 12:54
  • @terdon Linux (Debian 7) – learnerX Mar 29 '15 at 12:54
  • One would think the -p would work for leaving out line numbers and ASCII, but it only works for hexadecimal output, not binary (as noted on the man page: The command line switches -r, -p, -i do not work with this mode.). – Peter Mortensen Jun 11 '17 at 12:57
6

It's simple enough to parse the output into the format you want:

xxd -b /root/Desktop/image.png | cut -d: -f 2 | sed 's/  .*//; s/ //g'

The cut will remove the line numbers and the sed will first remove the last column (s/ .*// will remove everything that comes after two consecutive spaces) and then removes all single spaces.

You could also use awk:

xxd -b ~/a.png | awk '{print $2$3$4$5$6$7}'

Or Perl:

xxd -b ~/a.png | head -1 | perl -lane 'print join "",@F[1..6]'

Or coreutils:

xxd -b ~/a.png | cut -d" " -f2-7 | tr -d ' '
  • Awesome, that did it. I keep telling myself to learn 'awk' and 'sed' but somehow I just never did. I will start learning it soon. – learnerX Mar 29 '15 at 13:02

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.