RE: sprintf()
Brought to you by:
jmarshall
From: ET T. <et...@mi...> - 2001-09-19 09:08:28
|
oh my... seems that i've forgotten everything from school thanks Jean. > -----Original Message----- > From: Jean Delvare [mailto:de...@en...] > Sent: Wednesday, 19 September 2001 5:00 PM > To: ET Tan > Cc: Prc-Tools-Devel > Subject: Re: sprintf() > > > > > Disregard my last message. Can anyone tell me what is wrong with the > > following:- > > > > ---- > > unsigned long ix = 2220000; // a 32-bit variable > > char hexStr[100]; > > > > MemSet(hexStr, 100, 0); > > > > memset(hexStr, 0, 50); > > StrIToH(hexStr+StrLen(hexStr), (ix >> 24) & 0xff); > > hexStr[StrLen(hexStr)] = ':'; > > StrIToH(hexStr+StrLen(hexStr), (ix >> 16) & 0xff); > > hexStr[StrLen(hexStr)] = ':'; > > StrIToH(hexStr+StrLen(hexStr), (ix >> 8) & 0xff); > > hexStr[StrLen(hexStr)] = ':'; > > StrIToH(hexStr+StrLen(hexStr), (ix) & 0xff); > > hexStr[StrLen(hexStr)] = ':'; > > > > WinDrawChars(hexStr, StrLen(hexStr), 5, 20); > > ---- > > > > The output was 00000000:00000021:000000DF:000000E0. The > prefixed zeros are > > mysterious, anyway, that aside, shouldn't it be > > 000000E0:000000DF:00000021:00000000 since motorola dragonball > is big-endian? > > Calling both memset and MemSet is useless. The prefixed zeros are probably > normal. The result itself is perfectly coherent and does *not* depend on > the processor endianess. > > When you write (ix & 0xff), you don't get "the 8 bits on the right side" > of ix (nor on the left side). You get the 8 least significant bits, > regardless of the processor endianess. This is pure mathematics, this is > not hardware related. > > > ---- > > unsigned long ix = 2220000; // a 32-bit variable > > char hexStr[100]; > > > > MemSet(hexStr, 100, 0); > > sprintf(hexStr, "%x:%x:%x:%x", (ix >> 24) & 0xff, (ix >> 16) & > 0xff, (ix >> > > 8) & 0xff, ix & 0xff); > > > > WinDrawChars(hexStr, StrLen(hexStr), 5, 20); > > ---- > > > > The output was 0:0:0:21. I don't understand why. Could be that > I was using > > sprintf() the wrong way?. > > Yes, the wrong way. You try to output a long using %x instead of %lx. If > you want to use %x, you have to cast each parameter to UInt16 or UInt8. > > sprintf(hexStr, "%x:%x:%x:%x", (UInt8)(ix >> 24) & 0xff, (UInt8)(ix >> > 16) & 0xff, (UInt8)(ix >> 8) & 0xff, (UInt8)ix & 0xff); > > You should be using StrPrintF instead of sprintf. Also, please note that > the MemSet call is useless here. > > Hope it helps. > > -- > /~~ Jean "Khali" Delvare > -----\_ mail: de...@en... > --------\ http://www.ensicaen.ismra.fr/~delvare/ > ---=ISMRA/- ____________________________________________________ > |