Search code examples
cbashhexdump

Print a file in binary form (1 and 0)


In the context of a GZip decoder I need a unix tool or a C solution to print my compressed gzip file on my screen in a binary form. What I exactly need is hexdump able to print in binary instead of octal,decimal or hexadecimal.

I am pretty sure this tool exists but I can't find it :-(

EDIT I guess such editor is hard to find because the use case must not be frequent. In my case I have very small files and need for debug purpose to search for binary "patterns" such as 10010011 that may cross several bytes


Solution

  • perl -ne 'print unpack(B8,$_),$/for split//' FILE
    

    or to have 8 elements in a line

    perl -ne 'print unpack(B8,$_),++$i%8?" ":"\n"for split//;END{print"\n"}' 
    

    without linebreaks

    perl -ne 'print unpack(B8,$_)for split//' FILE