Andrew Bell wrote:
> I want to take a floating point number and determine it's binary
> represention. This seems easy enough as the answer is right there in
> memory. I can do this with a debugger, but other than dicking around with
> memcopies, I can't see an easy way to print out the memory contents of
> arbitrary data.
#include <stdio.h>
int main(void)
{
float f = 903996940.376776;
double d = 903996940.376776;
printf("%08x\t%08x%08x\n",
*(int *)&f,
((int *)&d)[1], ((int *)&d)[0]
);
return 0;
}
> So here is what I have thus far (I know, it's a disgusting conglomeration of
> C and C++ but that's how I code).
> Here is the output
> 1 1 4e578798 6303a32 0
> What seems wrong is that &d and (&d)+4 have the same value (and it's
> apparently 1 ??).
The output which I get is:
0xbffff834 0xbffff854 4e578798 6303a32 bffff864
which is correct.
> Because doubles are 64-bit, I tried to partition the
> memcopy into two separate ints. However, I've done something silly whilst
> trying to increment 4 bytes in memory. It seems like simple pointer
> arithmetic to moi, so I'll ask some good coders -- what gives?
Note that:
(char *)(&x + n)
is equivalent to
(char *)&x + n * sizeof(x)
IOW, you need to cast pointers to `char *' if you want to add an
offset which is in bytes (rather than in multiples of the size of the
type of the object).
Also, there is no need to use memcpy() for this; you could replace
memcpy ( (void*)&i_f, (void*)&f,4);
memcpy ( (void*)&i_d1, (void*)&d,4);
memcpy ( (void*)&i_d2, (void*)((&d)+4),4);
with
i_f = *(int *)&f;
i_d1 = *(int *)&d;
i_d2 = *((int *)&d + 1);
--
Glynn Clements <[EMAIL PROTECTED]>