tags:

views:

33

answers:

1

Hello everyone,

I have a bit of trouble with the following code:

void main()
{

float value = 100;

char * vP;

vP = (char *) &value;

printf ("%02x ", *(vP+3));
printf ("%02x ", *(vP+2));
printf ("%02x ", *(vP+1));
printf ("%02x ", *(vP+0));

}

The output I get is:

42 ffffffc8 00 00

instead of:

42 c8 00 00 (as required for an IEEE 32bit conversion)

Can anyone help and explain what goes wrong ? If I use a float value of e.g. 12.2 everything is fine !

Thanks and best regards

Olaf.

+3  A: 

That's because a char is signed on your machine. Then, in printf, because it's a variadic function, the char is promoted to an int keeping the negative sign. Therefore the 0xC8 becomes 0xFFFFFFC8.

Use an unsigned char* vP for force the representation to be unsigned. See http://www.ideone.com/WPQ4D for a comparison.

KennyTM
Or, use `%02cx` as the format spec.
Potatoswatter