/* This is for the FILE*, and fprintf */
#include <stdio.h>

void memPrint(FILE *fp, unsigned char * addr, int len) {
  /* miscellaneous variables, mostly just counting vars. */
  int i, j = 0, k = 0, l = 0;

  for (i = 0;  i < len;) {
    /* 
     * here we are counting in blocks of 16, so that each row in the 
     * table will be labeled, and will have exactly 16 entries, one for 
     * each byte in the address range. 
     */
    /* The Row label */
    fprintf(fp, "0x%x:   ", ((unsigned int)addr)+((unsigned int)i));

    /* The row contents */
    for (j = 1, k = 0;k != 16; i++, k++, j++) {
      fprintf(fp, "%2.2x", *(addr+i));
      if (j % 2 == 0)
        fprintf(fp, " ");
    }

    /* Now show what characters are in the memory locations of this row.  */
    fprintf(fp, "\"");
    { unsigned char * Na = addr+l;
      for (;l < i; Na++, l++)
        /* Make sure that the character is a printable character! */
        if (*Na >= 0x20 && *Na <= 0x7e)
          fprintf(fp, "%c", *Na);  /* Force convert of number to character */
        else 
        /* 
         * if the character is not printable, then simply print a floating 
         */
          fprintf(fp, "%c", 0xb7);
    }
    fprintf(fp, "\"\n");
  }
}

