打印文件的二进制格式,C代码:
1 #include <stdio.h> 2 3 unsigned char buff[1024]; 4 5 void print_bin(unsigned char *buff, int i) 6 { 7 int shirt = 128; 8 for (int k = 0; k < 8; k++) { 9 if (shirt & buff[i]) { 10 printf("1"); 11 } 12 else 13 printf("0"); 14 shirt = shirt >> 1; 15 } 16 } 17 18 int main(int argc, char ** argv) 19 { 20 if (argc < 2) { 21 printf("please input the file name!\n"); 22 return 0; 23 } 24 FILE * fp = fopen(argv[1], "rb"); 25 int len = 0; 26 int acc = 0; 27 while ((len = fread(buff, 1, 1024, fp)) > 0) { 28 printf("%d\n", len); 29 for (int i = 0; i < len; i++) { 30 print_bin(buff, i); 31 printf(" "); 32 acc++; 33 } 34 if (acc % 10 == 0) printf("\n"); 35 } 36 printf("\n"); 37 fclose(fp); 38 return 0; 39 }