printf in dump routine

Terry Franklin Byrum byrum at cs.odu.edu
Thu Mar 2 01:19:56 AEST 1989




Try this instead...

void print_data(pnt,num)
unsigned char *pnt;
int num;
{
  int i;
  for(i = num - 1; i >= 0; i--)
     printf("%02x ",*(pnt + i));
}

> ... There I got 'ffd2040000', which except for the leading 'ff' looks 
> like it was printing the bytes backwards. Any clues ...

Have you ever heard of low-high (Little Endian) architecture.  Most PC
type computers store values least significant byte first.  If you want
a number written (normally) you must scan backwards.

Also, on most PC machines char is signed by default.  Due to sign extension,
if the byte value is greater than 7F Hex, unwanted leading FFs will appear
on promotion to int.  The solution is to specify unsigned char* instead of
char*, either as the function's formal parameter or as a cast within the
printf argument.

		... Frank

Frank Byrum, BROOKS Financial Systems, Suffolk VA, (804)539-7202  1st Law of SE
INET:byrum at cs.odu.edu UUCP:..!sun!xanth!{byrum,brooks!frank}  Semper Sic Factum
-- 

	Inventions have long since reached their limit --
	and I see no hope for further developments.
		- Julius Frontinus, world famous engineer, Rome, 10 AD

                                |  Terry Franklin (Frank) Byrum
                                |  BROOKS Financial Systems, Inc.
   ___                          |  INET:  byrum at cs.odu.edu
  /__  _.  __. _ _  /_          |  UUCP:  ...!sun!xanth!brooks!byrum 
 /   _/ \_(_/|_|\|_/ \          |  DISCLAIMER: No one listens anyway! 



More information about the Comp.lang.c mailing list