A
A. Farber
Hello,
I'm trying to print out a const char* buffer
by first printing 16 bytes as hex codes,
then printing them again as characters
or dots (if they aren't printable) and so on:
20 A5 96 74 00 00 00 00 05 AD 4A 7C D3 FF 70 00 ..t......J|..p.
31 B0 66 7F 9F 3D AC 00 9D FF C2 02 AB 10 28 76 1.f..=........(v
49 9A 5E 7C F7 3D E4 00 35 F9 3A 02 83 00 E0 76 I.^|.=..5.:....v
Right now it's too late here in Germany
and I have come up with this fugliness only:
void
dump_buffer(unsigned n, const unsigned char* buf)
{
const unsigned char *p, *end;
unsigned i, j;
end = buf + n;
for (i = 0; ; i += 16) {
p = buf + i;
for (j = 0; j < 16; j++) {
fprintf(stderr, "%02X ", p[j]);
if (p + j >= end)
goto BREAKOUT;
}
fprintf(stderr, " ");
p = buf + i;
for (j = 0; j < 16; j++) {
fprintf(stderr, "%c", isprint(p[j]) ? p[j] :
'.');
if (p + j >= end)
goto BREAKOUT;
}
fprintf(stderr, "\n");
}
BREAKOUT:
return;
}
I know that it's horrible. Maybe I'll do better tomorrow.
Does maybe any kind soul has a nice solution
for this probably often encountered problem?
I've looked at the Vim's xxd source code, but
it is too much for me to understand right now...
Thank you
Alex
I'm trying to print out a const char* buffer
by first printing 16 bytes as hex codes,
then printing them again as characters
or dots (if they aren't printable) and so on:
20 A5 96 74 00 00 00 00 05 AD 4A 7C D3 FF 70 00 ..t......J|..p.
31 B0 66 7F 9F 3D AC 00 9D FF C2 02 AB 10 28 76 1.f..=........(v
49 9A 5E 7C F7 3D E4 00 35 F9 3A 02 83 00 E0 76 I.^|.=..5.:....v
Right now it's too late here in Germany
and I have come up with this fugliness only:
void
dump_buffer(unsigned n, const unsigned char* buf)
{
const unsigned char *p, *end;
unsigned i, j;
end = buf + n;
for (i = 0; ; i += 16) {
p = buf + i;
for (j = 0; j < 16; j++) {
fprintf(stderr, "%02X ", p[j]);
if (p + j >= end)
goto BREAKOUT;
}
fprintf(stderr, " ");
p = buf + i;
for (j = 0; j < 16; j++) {
fprintf(stderr, "%c", isprint(p[j]) ? p[j] :
'.');
if (p + j >= end)
goto BREAKOUT;
}
fprintf(stderr, "\n");
}
BREAKOUT:
return;
}
I know that it's horrible. Maybe I'll do better tomorrow.
Does maybe any kind soul has a nice solution
for this probably often encountered problem?
I've looked at the Vim's xxd source code, but
it is too much for me to understand right now...
Thank you
Alex