Skip to content

Instantly share code, notes, and snippets.

@madex
Created October 20, 2014 09:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madex/8c134274d6d7a859b42c to your computer and use it in GitHub Desktop.
Save madex/8c134274d6d7a859b42c to your computer and use it in GitHub Desktop.
hexdump that requires only a putchar() function, for microcontoller
void hexDump(char *description, void *basisAddr, void *startAddr, unsigned long len) {
unsigned char i = 0;
unsigned long tempAdr;
char hex[] = "0123456789abcdef";
unsigned char buff[17], *ptrBuf;
unsigned char *pc = (unsigned char*) startAddr;
if (description != NULL) {
while (*description)
putchar(*description++);
putchar('\n');
}
while (len--) {
if (i >= 16) {
i = 0;
putchar(' ');
putchar(' ');
ptrBuf = buff;
while (*ptrBuf)
putchar(*ptrBuf++);
putchar('\n');
}
if (i == 0) {
putchar(' ');
putchar(' ');
tempAdr = pc - (unsigned char*) basisAddr;
putchar(hex[(tempAdr >> 28) & 0xf]);
putchar(hex[(tempAdr >> 24) & 0xf]);
putchar(hex[(tempAdr >> 20) & 0xf]);
putchar(hex[(tempAdr >> 16) & 0xf]);
putchar(hex[(tempAdr >> 12) & 0xf]);
putchar(hex[(tempAdr >> 8) & 0xf]);
putchar(hex[(tempAdr >> 4) & 0xf]);
putchar(hex[tempAdr & 0xf]);
putchar(' ');
}
putchar(' ');
putchar(hex[(*pc >> 4) & 0xf]);
putchar(hex[*pc & 0xf]);
if ((*pc < 0x20) || (*pc > 0x7e))
buff[i] = '.';
else
buff[i] = *pc;
buff[i + 1] = '\0';
pc++;
i++;
}
while (i++ < 16) {
putchar(' ');
putchar(' ');
putchar(' ');
}
putchar(' ');
putchar(' ');
ptrBuf = buff;
while (*ptrBuf)
putchar(*ptrBuf++);
putchar('\n');
}
@annazolkieve
Copy link

annazolkieve commented Jun 18, 2020

Thanks! But what is the basisAddr? What value to use for basisAddr? NULL?
Usually hexdump is defined as: void hexdump(void *mem, unsigned int len).

@madex
Copy link
Author

madex commented Jun 23, 2020

startAddr is like mem
with basisAddr, you decide if you want the memory address to absolute if basisAddr = 0, or relative then you can change basisAddr = mem.

@annazolkieve
Copy link

Thanks! I use NULL then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment