Thread: Help printf desired number of characters

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  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, 05:07 PM