Last active
May 6, 2021 17:29
-
-
Save IMelker/0cf615b4a450507ead4d12750fafa2fe to your computer and use it in GitHub Desktop.
Dump bytes to cstring
This file contains hidden or 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
#include <cstring> | |
#include <cstdio> | |
void dump_bytes(const unsigned char * data, int len, char *outBuf) { | |
sprintf(outBuf, "%d bytes:\n", len); | |
char * tmp=outBuf + strlen(outBuf); | |
for (int j=0; j<len; j++) { | |
if (j % 16 == 0) { | |
sprintf(tmp, "%04X:", j); | |
tmp+=5; | |
} | |
sprintf(tmp, " %02X", data[j]); | |
tmp += 3; | |
if (j % 8 == 7) | |
*tmp++ = ' '; | |
if (j % 16 == 15 || j == len - 1) { | |
*tmp++ = ' '; | |
for (int k = j - j % 16; k <= j; k++) { | |
*tmp++ = (data[k] <= 32 ? '.' : data[k]); | |
if (k % 16 == 7) | |
*tmp++ = ' '; | |
} | |
*tmp++ = '\n'; | |
} | |
} | |
*tmp++ = '\n'; | |
*tmp++ = 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment