473,473 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Printing a hex dump of a buffer

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
Jan 24 '08 #1
5 27829

"A. Farber" <Al**************@gmail.comwrote in message
news:b1**********************************@v17g2000 hsa.googlegroups.com...
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
Untested but

void dumpbuff(char *buff, int N)
{
int i;

for(i=0;i<N;i+=16
if(i + 16 <= N)
printline(data+i, 16);
else
printline(data+1, N - i);
}

void printline(char *data, int N)
{
int i;

for(i=0;i<N;i++)
printf("%02x ", data[i]):
printf(" ");
for(i=0;i<N;i++)
if(isgraph((unsigned) data[i]))
printf("%c", data[i]);
else
printf(".");
printf("\n");
}

As long as you break things up the right way the function can look nice and
clean.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jan 24 '08 #2
"A. Farber" <Al**************@gmail.comwrites:
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
<snip>
Does maybe any kind soul has a nice solution
for this probably often encountered problem?
I'd do this:

void dump_buffer(unsigned n, const unsigned char* buf)
{
int on_this_line = 0;
while (n-- 0) {
fprintf(stderr, "%02X ", *buf++);
on_this_line += 1;
if (on_this_line == 16 || n == 0) {
int i;
fputs(" ", stderr);
for (i = on_this_line; i < 16; i++)
fputs(" ", stderr);
for (i = on_this_line; i 0; i--)
fputc(isprint(buf[-i]) ? buf[-i] : '.', stderr);
fputs("\n", stderr);
on_this_line = 0;
}
}
}

--
Ben.
Jan 25 '08 #3
Thank you all!
Jan 25 '08 #4
Peter Nilsson <ai***@acay.com.auwrites:
"A. Farber" <Alexander.Far...@gmail.comwrote:
<snip>
>fprintf(stderr, "%c", isprint(p[j]) ? p[j] : '.');

Note that isprint() for \n, \t, \b, \r, etc... are likely
to be true.
That is at odds with my reading of the standard (specifically
paragraph 3 of 7.4). Am I misunderstanding what you mean?

--
Ben.
Jan 25 '08 #5
Ben Bacarisse <ben.use...@bsb.me.ukwrote:
Peter Nilsson <ai...@acay.com.auwrites:
"A. Farber" <Alexander.Far...@gmail.comwrote:
fprintf(stderr, "%c", isprint(p[j]) ? p[j] : '.');
Note that isprint() for \n, \t, \b, \r, etc... are likely
to be true.

That is at odds with my reading of the standard (specifically
paragraph 3 of 7.4). *Am I misunderstanding what you mean?
No, I was wrong. Thanks for the correction.

--
Peter
Jan 26 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ravi Tallury | last post by:
Hi We are having issues with our application, certain portions of it stop responding while the rest of the application is fine. I am attaching the Java Core dump. If someone can let me know what...
3
by: PFMcCracken | last post by:
We have a database search page written in ASP. One of the information pages does not print properly because the windows background printing reloads the page, but does not wait long enough to...
4
by: ma740988 | last post by:
A few days ago I recieved yet again advice on implementing a buffer of bytes. At issue: (part 1) how do I take the contents of a struct, then dump (used sparingly) it into a byte buffer. ...
2
by: jim_geissman | last post by:
I have a script or SP that takes a very long time to perform multiple tasks, and after each one there is a PRINT statement that shows the time and what was just accomplished, to help me monitor...
10
by: Jeff B. | last post by:
Has anyone come across a decent algorithm for implementing word wrap features in .net printing? I have a small component that uses basic printing techniques (i.e. e.Graphics.DrawString in a...
5
by: Senapathy | last post by:
Environment: WinXP, VC++ 7.1 Standard Edition ~~~~~~~~~~~ I have a set of functions that I invoke from main() function of a sample console app. These are the functions:...
0
by: mottebelke | last post by:
I want to print a bitmap directly to a network printer using a socket. To do this I use PCL codes to set the printer in the right mode and print the image. Note that this code is used on the...
2
by: Iam25boy | last post by:
Hi all, I am experiencing a really weird core dump issue on Solaris 8. The program was compiled with g++ and it is a multi-threaded program. Here is the stack: #0 0x7fb40890 in memcpy ()...
2
by: laurent.pauloin | last post by:
Hello I have a buffer of char and I want to print in a 64 bit word in hexa. charbuff0 charbuff1 charbuff2 charbuff3 charbuff4 charbuff5 charbuff6 charbuff7 64bit msb 64bit lsb
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.