Thread: Help adding leading zero's to a char!!

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    58

    Help adding leading zero's to a char!!

    Hi all;

    I basically want to add leading zero's to a char when going from int to a char in snprintf.

    See example :

    Code:
    unsigned char len[2];
    int length = 144;
    
    snprintf(len, 2, "02%d", length);  /*this would add leading zeros to the integer*/
    
    snprintf(len, 2, "02%c", length);  /*this doesnt work for a char, any ideas?
    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well a char will only ever be one char, so you should perhaps try "0%c"

    But your array is too small to fit two characters AND a \0.
    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
    May 2012
    Location
    Arizona, USA
    Posts
    948
    First off, the correct format string to add leading zeroes to an integer is "%02d". Note that with your example (but with the corrected format string), since you are passing in 2 as the size of the buffer, the string will contain "1", which is one character plus a null terminator.

    Second, what does it mean to add leading zeroes to a char? "%c" gives you a single character. If you want a leading zero in front of that, use "0%c". But again, "%c" is just a character, not a decimal integer or anything like that. For example, if you tried to print "0%c" to a string with the character 65, you would get the string "0A" (but by passing a size of 2 to snprintf, the string would always be "0"). Is that what you want? If not you need to be more specific about what you actually want to do.

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    58
    Thanks for the replies .

    Basically my unsigned char array will hold 0x00 0x00
    if value is 144 Dec i like it to be 0090 in array in hex. so maybe i should use %x in sprintf?

    thks

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Try reading this page.

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Do you want a 16-bit big-endian binary value stored in your array?

    Code:
    sprintf(len, "%c%c", 0, length); // note: this is only one way to do this
    Or do you want a human-readable string like "0090"? That requires 5 bytes in your array (or 4 if you don't have a null terminator), plus reading the page that whiteflags linked to.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about adding integers to char
    By blueshift1980 in forum C++ Programming
    Replies: 6
    Last Post: 04-16-2009, 12:51 PM
  2. adding extra to char*
    By sujeet1 in forum C Programming
    Replies: 2
    Last Post: 10-17-2007, 12:21 PM
  3. Adding to char variables together
    By maxorator in forum C++ Programming
    Replies: 3
    Last Post: 10-02-2005, 10:27 AM
  4. adding a char variable to int one
    By yavanna in forum C# Programming
    Replies: 2
    Last Post: 12-11-2003, 03:05 PM
  5. Adding .txt to a char
    By ErionD in forum C++ Programming
    Replies: 11
    Last Post: 02-22-2002, 04:01 PM