Xref: utzoo comp.lang.c:16716 comp.sys.amiga:30165 Path: utzoo!attcan!lsuc!ncrcan!hcr!edwin From: edwin@hcr.UUCP (Edwin Hoogerbeets) Newsgroups: comp.lang.c,comp.sys.amiga Subject: Re: printf in dump routine Summary: the 6809 has a "sex" instruction. It gets executed *all* the time... Keywords: sex, printf, and the covert behaviour of C Message-ID: <5299@hcr.UUCP> Date: 4 Mar 89 03:50:16 GMT References: <652@dsacg2.UUCP> Reply-To: edwin@hcrvax.UUCP (Edwin Hoogerbeets) Organization: HCR Corporation, Toronto Lines: 43 In article <652@dsacg2.UUCP> nor1675@dsacg2.UUCP writes: >I'm trying to pass a pointer to a variable >that I want to dump in hex and the number of bytes to print: > > long test_long; > test_long = 1234; > print_data((char *)&test_long,sizeof(test_long)); > void print_data(pnt,num) > char *pnt; > int num; > { > int i; > for(i = 0; i < num; i++) > { > printf("%02x",*(pnt + i)); > } > } Your problem is that *(pnt + i) has type char. Thus, a char is pushed onto the stack and printf is called, right? Well, not really. First, the char is promoted to a int, which under Lattice is 32 bits. But wait! A char is a signed quantity, so the promotion dutifully sign extends the char. Any character >= 128 has the high bit set, and this bit gets copied during sign extension. You got "000004ffffffd2" because the "d2" has the high bit set (and extended in the call to printf) while the other bytes don't have the high bit set. Your solution is simple: Change your argument declaration to unsigned char *pnt; or better yet, register unsigned char *pnt; How's that for a declaration! Happy hacking, Edwin HCR co-op subterfuge team