home *** CD-ROM | disk | FTP | other *** search
/ ftp.ee.pdx.edu / 2014.02.ftp.ee.pdx.edu.tar / ftp.ee.pdx.edu / pub / users / Harry / Blitz / BlitzSrc / hexdump.c < prev    next >
C/C++ Source or Header  |  2007-09-20  |  3KB  |  135 lines

  1. /**********
  2.  
  3.    hexdump  -  Harry Porter
  4.  
  5.    This program reads a file from the standard input containing
  6.    arbitrary hex data and outputs it to the standard output
  7.    in a format suitable for human examination.  Unlike various
  8.    versions of 'od', it prints the bytes in exactly the order read.
  9.  
  10.    This program is most useful for examining files that contain either
  11.    unprintable characters or characters (like CR/NL and TAB/SPACE)
  12.    that are easily confused.
  13.  
  14.    This program runs on both big- and little-endian architectures.
  15.  
  16.    Here is an example of the using this program:
  17.  
  18.         % hexdump < hexdump.c
  19.         00000000:  2F2A 2A2A  2A2A 2A2A  2A2A 2A0A  0A20 2020    /**********..   
  20.         00000010:  6865 7864  756D 7020  202D 2020  4861 7272    hexdump  -  Harr
  21.         00000020:  7920 506F  7274 6572  0A0A 2020  2054 6869    y Porter..   Thi
  22.         00000030:  7320 7072  6F67 7261  6D20 7265  6164 7320    s program reads 
  23.         ...
  24.  
  25. **********/
  26.  
  27. #include <stdio.h>
  28.  
  29. int row [16];
  30. int size;
  31.  
  32. main () {
  33.    int addr;
  34.  
  35.    /* Each execution of this loop prints a single output line. */
  36.    addr = 0;
  37.    readline ();
  38.    while (size > 0) {
  39.      putlong (addr);
  40.      printf (":  ");
  41.      printline ();
  42.      addr = addr + 16;
  43.      readline ();
  44.    }
  45.  
  46. }
  47.  
  48.  
  49.  
  50. /* readline - This routine reads in the next 16 bytes from the file and
  51.               places them in the array named 'row', setting 'size' to
  52.               be the number of bytes read in.  Size will be less than
  53.               16 if EOF was encountered, and may possibly be 0.  */
  54. readline () {
  55. int c;
  56.   size = 0;
  57.   c = getchar ();
  58.   while (c != EOF) {
  59.     row [size] = c;
  60.     size = size + 1;
  61.     if (size >= 16) break;
  62.       c = getchar ();
  63.   }
  64. }
  65.  
  66.  
  67.  
  68. /* putlong - This routine is passed an integer, which it displays as 8
  69.              hex digits.  */
  70. putlong (i) {
  71.   putbyt ((i>>24) & 0x000000ff);
  72.   putbyt ((i>>16) & 0x000000ff);
  73.   putbyt ((i>>8) & 0x000000ff);
  74.   putbyt ((i>>0) & 0x000000ff);
  75. }
  76.  
  77.  
  78.  
  79. /* printline - This routine prints the current 'row'.  */
  80. printline () {
  81.   int i, c;
  82.   if (size > 0) {
  83.     i = 0;
  84.     while (i<16) {
  85.       if (i < size) {
  86.         putbyt (row[i]);
  87.       } else {
  88.         printf ("  ");
  89.       }
  90.       i++;
  91.       if ((i%2) == 0) {
  92.         putchar (' ');
  93.       }
  94.       if ((i%4) == 0) {
  95.         putchar (' ');
  96.       }
  97.     }
  98.     printf ("  ");
  99.     for (i=0; i<size; i++) {
  100.       c = row[i];
  101.       if ((c>=' ') && (c <= '~')) {
  102.         putchar (c);
  103.       } else {
  104.         putchar ('.');
  105.       }
  106.     }
  107.     printf ("\n");
  108.   }
  109. }
  110.  
  111.  
  112.  
  113. /* putbyt - This routine is passed a byte (i.e., an integer < 256) which
  114.           it displays as 2 hex characters.  If passed a number out of that
  115.           range, it outputs nothing.  */
  116. putbyt (c)
  117.    int c;
  118. {
  119.   int i;
  120.   if ((c >= 0) && (c <= 255)) {
  121.     i = (c & 0x000000f0) >> 4;
  122.     if (i < 10) {
  123.       putchar ('0' + i);
  124.     } else {
  125.       putchar ('A' + i - 10);
  126.     }
  127.     i = (c & 0x0000000f) >> 0;
  128.     if (i < 10) {
  129.       putchar ('0' + i);
  130.     } else {
  131.       putchar ('A' + i - 10);
  132.     }
  133.   }
  134. }
  135.