Created
October 20, 2014 09:55
-
-
Save madex/8c134274d6d7a859b42c to your computer and use it in GitHub Desktop.
hexdump that requires only a putchar() function, for microcontoller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
} |
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
.
Thanks! I use NULL then.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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).