Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // see: see the physical bits in a file
- // usage: see [filename]
- // sort of like the `od' command, but only supports binary output
- // $ echo foobar > foo.txt; see foo.txt
- // 01100110 01101111 01101111 01100010
- // 01100001 01110010 00001010
- // $
- // bugs:
- // - assumes big-endianness of machine
- // - only allows one file to be seen at a time
- // - no command line options
- // - generally untested
- #include <stdio.h>
- int main(int argc, char **argv)
- {
- FILE *fp;
- if (argc > 1)
- {
- if ((fp = fopen(argv[1], "r")) == NULL)
- {
- fprintf(stderr, "see: could not open file `%s': ");
- perror(NULL);
- return -1;
- }
- }
- else
- fp = stdin;
- unsigned currbyte;
- signed currbit;
- int col = 0;
- int maxcols = 4;
- while ((currbyte = fgetc(fp)) != EOF)
- {
- for (currbit = 7; currbit >= 0; currbit--)
- {
- if ( (currbyte >> currbit) & 1 )
- putchar('1');
- else
- putchar('0');
- }
- putchar(' ');
- if (++col == maxcols)
- {
- col = 0;
- putchar('\n');
- }
- }
- if (col > 0)
- putchar('\n');
- return 0;
- }
Add Comment
Please, Sign In to add comment