CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2007
    Posts
    52

    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

  2. #2
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    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

  3. #3
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: printf messing up hex numbers

    instead of taking an int as argument, my guess would be to use an unsigned char.

  4. #4
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: printf messing up hex numbers

    Quote Originally Posted by Richard.J View Post
    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.

  5. #5
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    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
  •  





Click Here to Expand Forum to Full Width

Featured