printf("04x",short) - negitive hex giving me problems

This is a discussion on printf("04x",short) - negitive hex giving me problems within the C Programming forums, part of the General Programming Boards category; Hello all, First I want to thank everyone who answer my previous question. I think that's the fastest response I ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    7

    printf("04x",short) - negitive hex giving me problems

    Hello all,

    First I want to thank everyone who answer my previous question. I think that's the fastest response I ever had on the web.

    Well I'm back again with more questions for this class assignment. We have to write a function to do a so-called "memory dump" (but instead of a text file, we are just dumping straight into the command prompt)

    so we need to produce an output like this:
    Code:
    0x3000: 2c39 e03a f022 da01 0e03 000b 5020 c1c0  ,9.:."......P ..
    0x3008: 1b61 2031 1140 0cd9 f025 6200 0423 72c0  .a 1.@...%b..#r.
    0x3010: 1021 16e1 0ffa 1dbf 7f80 2056 2220 1009  .!........ V" ..
    and instead I get this:
    Code:
    0x3000: 2c39 ffffe03a fffff022 ffffda01 0e03 000b 5020 ffffc1c0 9,:."....... P..
    0x3008: 1b61 2031 1140 0cd9 fffff025 6200 0423 72c0 a.1 @...%..b#..r
    0x3010: 1021 16e1 0ffa 1dbf 7f80 2056 2220 1009 !.........V  "..
    So you can see the negative hex numbers are sign-extended... which looks bad. currently I'm using this code to print out each hex number:
    printf("%04x ",*shortPointer);

    any simple way to force it not to sign extend?

    Thanks,
    Kairos

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,648
    Variable arguments go through a implicit conversion to int. So you're going from short->int which is where your sign extension is coming from. To prevent the sign extension, make it an unsigned short first.

    printf("%04x ", (unsigned short)*shortPointer);

    gg

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    7
    Quote Originally Posted by Codeplug View Post
    Variable arguments go through a implicit conversion to int. So you're going from short->int which is where your sign extension is coming from. To prevent the sign extension, make it an unsigned short first.

    printf("%04x ", (unsigned short)*shortPointer);

    gg
    Wow thanks. It was staring me in the face but I couldn't see it. I think I have all of the bug now ironed out of this assignment.

    Thanks again,
    Kairos

    P.S. how do I mark topics as answered/completed

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by KairosDrasis View Post
    P.S. how do I mark topics as answered/completed
    This isn't StackOverflow Just don't post again in the topic and it'll disappear after a while...

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. Hex dump
    By Banana Man in forum C Programming
    Replies: 17
    Last Post: 01-06-2008, 10:03 AM
  3. Replies: 2
    Last Post: 09-24-2007, 02:46 AM
  4. Binary to Hex Conversion
    By elrookie in forum C Programming
    Replies: 8
    Last Post: 06-26-2007, 10:58 AM
  5. Single hex dump - Error codes / Plain errors...
    By Blackroot in forum Windows Programming
    Replies: 4
    Last Post: 04-03-2007, 03:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21