printf messing up hex numbers
So, I have a hex file that I read into a vector. and now I'm printing out it's contents by doing a good ol' printf. When it does this I am surprised to see vaules that were FF in my hex editor come up as FFFFFF80, FFFFFFFF, FFFFFF98 and other things like that. I assumed that maybe the hex editor, when it displayed them, just left off the end to these numbers, so I did a printf("%.2X",hexnumber); and that gave me the same result. Why is this happening and why are the FF's in my editor being displayed as something else in the program?
Re: printf messing up hex numbers
Without seeing soms code it's not clear what you want to do.
Something like this ?
Code:
#include <stdio.h>
int main() {
int x = -128;
printf("=%2X\n", x & 0xff);
}
Kurt
Re: printf messing up hex numbers
instead of taking an int as argument, my guess would be to use an unsigned char.
Re: printf messing up hex numbers
Quote:
Originally Posted by
Richard.J
instead of taking an int as argument, my guess would be to use an unsigned char.
Hexadecimal is just a different representation of the underlying data type - may it be an integer, character, pointer etc.
Re: printf messing up hex numbers
that is of course correct, but if you try to printf an integer, the result may vary depending on the value input. I. e. a negative integer or a byte value with the MSB set may be prepended by 0xFF