home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / zip / language / sozosun.zoo / hex.c next >
C/C++ Source or Header  |  1991-02-09  |  1KB  |  52 lines

  1. /*****************************************************/
  2. /* simple program to do a hex dump of a file         */
  3. /*****************************************************/
  4. #include <stdio.h>
  5.  
  6. main(argc,argv,envp)
  7. int argc;
  8. char **argv, **envp;
  9.     {
  10.     FILE *fp;
  11.     int col,pos;
  12.         char buf[33];
  13.  
  14.     /* for each file on the command line */
  15.     while(argc-- > 1) {
  16.         if((fp=fopen(argv[argc],"rb")) == NULL) {
  17.             printf("hex: warning - unable to open file %s\n",argv[argc]);
  18.             continue;
  19.             }
  20.  
  21.         printf("hex: dumping file %s\n\n",argv[argc]);
  22.  
  23.             /* read and dump the file */
  24.         pos=0;
  25.         while(feof(fp) == 0) {
  26.             printf("%4.4x | ",pos);
  27.             for(buf[0]=fgetc(fp),
  28.                             buf[1]=fgetc(fp),
  29.                 col=1; 
  30.                 col < 16;
  31.                 pos++,
  32.                 buf[++col] = fgetc(fp),
  33.                 buf[++col] = fgetc(fp)) {
  34.                 printf("%4.4x ",(unsigned short)
  35.                        (buf[col-1]*256+buf[col]));
  36.                 }
  37.             printf(" | ");
  38.             for(col=0; col <= 16; col++) {
  39.                 if(buf[col] >= 0x20 && buf[col] <= 0x7e) {
  40.                     printf("%c",buf[col]);
  41.                     }
  42.                 else {
  43.                     printf(" ");
  44.                     }
  45.                 }
  46.             printf("\n");
  47.             }
  48.         fclose(fp);
  49.         }
  50.     }
  51.  
  52.