printf messing up hex numbers
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: printf messing up hex numbers

  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,112

    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.
    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds


    Article(s): Allocators (STL) Function Objects (STL)

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

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center