Help printf desired number of characters

This is a discussion on Help printf desired number of characters within the C Programming forums, part of the General Programming Boards category; I am trying to printing a short signed whose values range from -32k to 32k. And hence I am using ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    83

    Help printf desired number of characters

    I am trying to printing a short signed whose values range from -32k to 32k. And hence I am using 0x%04X to shorten the length to 4 bytes. But there are some short values equal to -1 or negative and the hexadecimal format is instead printed to 0xFFFFFFFF.

    How can I remove those unnecessary 4 characters.
    Code:
    fprintf(hOutFile,"0x%04X, 0x%04X, %06d, %06d, %06d, 0x%X, 0x%X, %d, 0x%X, %d,  %d, %d, %d, %d, paddata, %d, %d, %d, %d, %d, 0x%X\n",
    						newPathFindBuff.Path[i].wField0x01,
    						newPathFindBuff.Path[i].wField0x02,
    						newPathFindBuff.Path[i].wX * 2,
    						newPathFindBuff.Path[i].wY * 2,
    						newPathFindBuff.Path[i].wZ * 2,
    						newPathFindBuff.Path[i].wField0x0A,
    						newPathFindBuff.Path[i].wField0x0C,
    						newPathFindBuff.Path[i].sbMedianWidth,
    						newPathFindBuff.Path[i].sbField0x0F,
    						newPathFindBuff.Path[i].bitCopsRoadBlock,
    						newPathFindBuff.Path[i].bitRestrictedAccess,
    						newPathFindBuff.Path[i].bitIgnoredNode,
    						newPathFindBuff.Path[i].bitPadFlags3,
    						newPathFindBuff.Path[i].bitUnkCount4To7,
    						newPathFindBuff.Path[i].bitSpeedLimit,
    						newPathFindBuff.Path[i].bitPadFlags13,
    						newPathFindBuff.Path[i].bitHaveUnrandomizeVehClass,
    						newPathFindBuff.Path[i].bitIsVehicleBoat,
    						newPathFindBuff.Path[i].byteSpawnRate,
    						newPathFindBuff.Path[i].byteField0x013);

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    Use something like "%04hx"
    printf - C++ Reference
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    Thanks a lot Salem!
    Solved

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-02-2011, 09:17 PM
  2. Displaying Numbers divisible by user's desired number
    By DaniiChris in forum C Programming
    Replies: 9
    Last Post: 07-07-2008, 02:06 PM
  3. printf conversion characters for long longs.
    By samus250 in forum C Programming
    Replies: 9
    Last Post: 04-13-2008, 08:37 PM
  4. How do I increment printf hex characters?
    By gechno in forum C Programming
    Replies: 2
    Last Post: 05-24-2004, 05:33 PM
  5. printf for all characters
    By volk in forum C Programming
    Replies: 2
    Last Post: 01-06-2003, 04:07 PM

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