Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > printf

Reply
Thread Tools

printf

 
 
Richard Heathfield
Guest
Posts: n/a
 
      08-03-2007
Ian Collins said:

> Shane wrote:


<snip>

>> Hmm, well bright spark, how do you think those packet sniffers were
>> written?
>>

> Long ago by people who knew what they were doing.


We still need people who know what they're doing. Curiosity is a *good*
thing.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
 
 
 
Kelsey Bjarnason
Guest
Posts: n/a
 
      08-07-2007
On Fri, 03 Aug 2007 10:51:45 +0000, Richard Heathfield wrote:

> Ian Collins said:
>
>> Shane wrote:

>
> <snip>
>
>>> Hmm, well bright spark, how do you think those packet sniffers were
>>> written?
>>>

>> Long ago by people who knew what they were doing.

>
> We still need people who know what they're doing. Curiosity is a *good*
> thing.


People who *don't* know what they're doing, but have sufficient curiosity
to learn, are likewise a good thing to have around.
 
Reply With Quote
 
 
 
 
David Thompson
Guest
Posts: n/a
 
      08-26-2007
On Tue, 31 Jul 2007 07:41:10 -0000, Mohan <(E-Mail Removed)> wrote:
<snip: rather confused query>
> assuming you can dump data in hex:
>

ITYM 'you' (the OP or whoever) can _read_ hex

> void xprint_binary_data (void *pointer_to_bin_data, int len)


In general 'int' is not the best type for length-of-memory values.
However, I personally don't want to read the dump of any item whose
length doesn't fit in the smallest permitted 'int' range.

> {
> unsigned int *t = (unsigned int *)pointer_to_bin_data;


A pointer to arbitrary data is not necessarily aligned correctly for
(unsigned) int, or in general for any noncharacter type, and there are
systems where this will indeed fail (trap or just give wrong data).

> int i, ui_len = (len / sizeof (unsigned int));
>

And there's even less reason to assume that all data to be dumped will
be an exact multiple of the size of int (or whatever). Even in cases
(platforms or particular buffers) where you can get away with uint or
whatever access, you need to check for and handle a residue -- or, if
'word' access works at all it almost certainly works for the rest of
the word after the 'valid' object, so just print one extra word,
perhaps with some indication how much of it is unwanted.

> for (i = 0; i < ui_len; i++)
> {
> printf ("%8.8x ", t[i]);


This assumes I32, which is common but not universal. It's
straightforward, but ugly, to fix it for the other common sizes.

It also assumes no padding bits in the representation of uint, which
is not strictly guaranteed but is in (current?) practice universal.

It's easier and safer to do bytes using unsigned char, and for a dump
routine the slight inefficiency will be swamped by the cost of I/O.
Only in pure-CPU operations like memcpy() and memcmp() is doing
wordwise operations SOMETIMES worth it -- and the ones in the standard
library like those two can be optimized by the implementor without
your having to write, debug, and maintain platform (and even compiler)
dependent code.

> if (i % 8 == 0) printf ("\n");


This produces a confusing output pattern: word 0 on first line, 1-8 on
second, 9-16 on third, etc. You want == 7. Or ~i % 8 == 0.

When doing bytewise, I like to use a technique (some might say trick)
to handle either within-the-line punctuation or end-of-line at once.
For your format (8 chunks of 4 bytes each per line):

for( i = 0; i < len; i++ ){
printf ("%02x", buf[i]); /* assumes C8; check to be safe,
or use (bleah!) "%0*x", (int)(CHAR_BIT+3)/4, buf[i] */
if( ++i == len ) putchar ('\n');
else if( i & 3 ) /* no separator */ ;
else putchar (i&0x1F? ' ' : '\n' );
}

> }
> printf ("\n");
> }
>

- formerly david.thompson1 || achar(64) || worldnet.att.net
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
printf affects following printf/s azza C Programming 0 10-17-2010 09:43 AM
Extracting printf(...) from (void) printf(....) guru Perl Misc 8 02-03-2009 10:37 PM
java printf Alex Polite Java 18 04-15-2006 04:09 AM
(void) printf vs printf whatluo C Programming 29 09-08-2005 05:42 PM
bus error with printf line included, error without printf line? ben C Programming 4 06-26-2004 04:42 PM



Advertisments