-
January 2nd, 2009, 10:00 AM
#1
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?
KTNXBYE
-
January 2nd, 2009, 10:10 AM
#2
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
-
January 3rd, 2009, 06:44 AM
#3
Re: printf messing up hex numbers
instead of taking an int as argument, my guess would be to use an unsigned char.
-
January 3rd, 2009, 11:20 AM
#4
Re: printf messing up hex numbers
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.
-
January 3rd, 2009, 11:51 AM
#5
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|