home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c006 / 1.ddi / DUMPM.C / CBTREE / LIBRARY / DUMPM.C
Encoding:
C/C++ Source or Header  |  1987-11-04  |  1.6 KB  |  75 lines

  1. /*
  2.  *
  3.  * dumpm.c   dump memory from ptr for num bytes, in hex and ASCII
  4.  *
  5.  */
  6.  
  7. #include "cbtree.h"
  8. #include <ctype.h>
  9.  
  10. #define hexc(x) '0' + ((x) > 9 ? (x) + 7 : (x))    /* hex nibble to ASCII */
  11.  
  12. void dumpm(ptr, num)
  13. char *ptr;
  14. int  num;                               /* number of characters tp dump */
  15. {
  16.     char *p = ptr;                      /* ascii pointer */
  17.     int   n  = num;
  18.     char  x, y;
  19.     char  displine[83];
  20.     char *dlp = displine;
  21.     register int j;
  22.  
  23.     while (num > 0)
  24.     {
  25.         sprintf(dlp, "\n%08lx: ", p);
  26.         dlp += 11;
  27.  
  28.         for (j = 16; j-- != 0; )          /* first hex */
  29.         {
  30.             if (n-- == 0)
  31.             {
  32.                repeat
  33.                {
  34.                    sprintf(dlp, "   ");        /* count exhausted */
  35.                    dlp += 3;
  36.                }
  37.                until(j-- == 0);
  38.                break;
  39.             }
  40.             else
  41.             {
  42.                 x = *p >> 4 & 0x0f;   /* display 2 hex digits */
  43.                 y = *p++ & 0x0f;
  44.                 sprintf(dlp, "%c%c ", hexc(x), hexc(y));
  45.                 dlp += 3;
  46.             }
  47.         }
  48.         sprintf(dlp, "  ");                /* then ascii */
  49.         dlp += 2;
  50.         for (j = 16; j-- != 0; )
  51.         {
  52.             if (num-- == 0)
  53.                break;
  54.             x = *ptr++ & 0x7f;
  55.             sprintf(dlp, "%c", isprint(x) ? x : '.');
  56.             ++dlp;
  57.         }
  58.         printf("%s", dlp = displine);
  59.     }
  60.     printf("\n");
  61. }
  62.  
  63. #ifndef NOTEST
  64.  
  65. void main()   /****   test harness   ****/
  66.  
  67. {
  68.  
  69.    dumpm(main,0x33);
  70.  
  71. }
  72.  
  73. #endif
  74.  
  75.