Skip to content

Instantly share code, notes, and snippets.

@IMelker
Last active May 6, 2021
Embed
What would you like to do?
Dump bytes to cstring
#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