Thread: Funny output on hex value of number

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    15

    Funny output on hex value of number

    Hi all,
    I'm trying to output the hex value of 255 to a read only dialog box in MFC. pdu[x]=255

    sprintf(temp_buffer,"%02x ",pdu[x]);
    output+=temp_buffer;

    Instead of getting ff, I'm getting ffffffff. Any ideas? Thanks
    Shav

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    1
    I might be way off here, but the hex value of the base-10 number 255 is 0xFF. It appears that you are passing the decimal number 255 to the dialog box. I might be reading this wrong though.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    15
    Yeah, I'm reading in an integer from an edit box. Is there any way to display the number as FF

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Instead of getting ff, I'm getting ffffffff. Any ideas? Thanks
    pdu[x] is signed, so it gets sign-extended all the way up to however many bits are in an integer.

    Making your pdu unsigned would do it, as would something like a cast, say
    sprintf( temp_buffer, "%02x ", (unsigned char)pdu[x] );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    15
    Brilliant, works a treat!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble making a function do what I want.
    By Shamino in forum C++ Programming
    Replies: 9
    Last Post: 12-07-2007, 11:20 AM
  2. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  3. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  4. string to hex
    By LogicError in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2005, 06:53 PM
  5. Replies: 4
    Last Post: 06-21-2002, 02:52 PM