#include <stdio.h>
#include <string.h>
#include <stdint.h>


static char temp[16];

#define PRINT_WORD(buf, w)\
        sprintf(temp, "%08X", w); \
		strcat(buf, temp);

#define PRINT_BYTE(buf, b)\
        sprintf(temp, "%02X", b); \
		strcat(buf, temp)

#define PRINT_STRING(buf, s)\
        sprintf(temp, "%s", s); \
		strcat(buf, temp)

#define PRINT_CHAR(buf, c)\
        sprintf(temp, "%c", c); \
		strcat(buf, temp)

void memdump(uint8_t *pBuf, uint32_t base, int num_bytes_per_row, int len)
{
	char buffer[1024] = {0};
	int i, j, cnt_hex, cnt_asc;
    uint8_t c;

    i = j = 0;
    cnt_hex = len;
    cnt_asc = len;
	
    do
    {
		buffer[0] = 0;
        PRINT_WORD(buffer, base + i);
        PRINT_STRING(buffer, ": ");
        for (j=0; j < num_bytes_per_row; j++)
        {
            if (cnt_hex)
            {
				PRINT_BYTE(buffer, pBuf[i+j]);
				PRINT_STRING(buffer, " ");
                cnt_hex--;
            }
            else
            {
                PRINT_STRING(buffer, "   ");
            }

        }
		
        PRINT_STRING(buffer, "   ");
        for (j=0; j < num_bytes_per_row; j++)
        {
            if (cnt_asc)
            {
                c = pBuf[i+j];
                if((c < 0x20) || (c > 0x7F))
                    c = '.';

                PRINT_CHAR(buffer, c);
                cnt_asc--;
            }
            else
            {
                PRINT_STRING(buffer, " ");
            }
        }
        printf("%s\n", buffer);
        i += num_bytes_per_row;

    } while (cnt_hex);
}