CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2013
    Posts
    32

    C Question: I want to convert an int to string of size n and pad with 0's.

    Hello,

    I have an int with a value say "1234" and I want that int to be a string like this with 8 bytes:

    Code:
    00001234
    I have this working with a string of size 8 like this:

    Code:
    int i = 1234;
    char str[9];
    sprintf(str, "%08d", i);
    However, can I make this dynamic in a way that what if I want to pad it to be size 10? Is there a way to pass a variable size to sprintf?

    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: C Question: I want to convert an int to string of size n and pad with 0's.

    Yes, e.g., use sprintf to format a string that is then used as the format.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jun 2013
    Posts
    32

    Re: C Question: I want to convert an int to string of size n and pad with 0's.

    lol super confused, can you provide example code?

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: C Question: I want to convert an int to string of size n and pad with 0's.

    I think part of the fun is trying this out yourself. I would expect something like this:
    Code:
    int i = 1234;
    char result[11];
    char format[6];
    int width = 10;
    /* call sprintf to format format here
       ... */
    snprintf(result, sizeof(result), format, i);
    Remember, you have a format string, and it could look like "%08d", or it could look like "%010d", etc. What format string could be used to format that format string?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: C Question: I want to convert an int to string of size n and pad with 0's.

    Look up for the '*' in the format string, like
    Code:
    sprintf(str, "%0*d", 8, i);
    (not tested, though). This should make you able to replace the hard-coded 8 by a variable.

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: C Question: I want to convert an int to string of size n and pad with 0's.

    This is a variation of the previous thread http://forums.codeguru.com/showthrea...tring-and-back

    If the number of chars required to represent i is greater then 8 then str still gets these extra chars - so buffer overflow is possible. IMO using sprintf() is a bad idea! Consider though something like this
    Code:
    #include <limits.h>
    #include <stdio.h>
    
    #define BUF_SIZE 11
    #define DIG_REQ 8
    
    int main()
    {
    	int i = INT_MIN;
    	char str[BUF_SIZE + 1] = { 0 };
    	int req = snprintf(NULL, 0, "%0*d", DIG_REQ, i);
    	snprintf(str, ((req <= BUF_SIZE) ? req : BUF_SIZE) + 1, "%0*d", DIG_REQ, i);
    	printf("%i\n%s\n", i, str);
    }
    here you specify both the number of digits required and the size of the buffer so that the buffer doesn't overflow and the output just gets truncated if there are too many digits to fit.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured