right padding

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • marc

    right padding

    Hello,

    Is it possible to right pad with "0" (or other character != blank) ?
    for example :
    1 , length 10 ="1000000000 "
    I've tried with sprintf but I can only left pad with "0" or right pad
    with blanks =>
    "0000000001 "
    "1 "

    Thanks.
  • Richard Heathfield

    #2
    Re: right padding

    marc said:
    Hello,
    >
    Is it possible to right pad with "0" (or other character != blank) ?
    for example :
    1 , length 10 ="1000000000 "
    I've tried with sprintf but I can only left pad with "0" or right pad
    with blanks =>
    "0000000001 "
    "1 "
    sprintf doesn't let you choose the padding character. But it's pretty easy
    to write a function to do the padding for you:

    #include <stdio.h>
    #include <assert.h>
    #include <limits.h>

    char *pad(char *s, int n, size_t width, int padch)
    {
    int count = 0;
    char *t = s;
    assert(width (sizeof n * CHAR_BIT + 5) / 3);
    count = sprintf(t, "%d", n);
    t += count;
    while(count < len)
    {
    *t++ = padch;
    }
    *t = '\0';
    return s;
    }

    In keeping with the spirit of comp.lang.c, the above is untested (because
    I'm in a hurry).

    --
    Richard Heathfield <http://www.cpax.org.uk >
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999

    Comment

    • s0suk3@gmail.com

      #3
      Re: right padding

      On Oct 21, 4:17 am, Richard Heathfield <rjh@see.sig.in validwrote:
      marc said:
      >
      Hello,
      >
      Is it possible to right pad with "0" (or other character != blank) ?
      for example :
      1 , length 10 ="1000000000 "
      I've tried with sprintf but I can only left pad with "0" or right pad
      with blanks =>
      "0000000001 "
      "1         "
      >
      sprintf doesn't let you choose the padding character. But it's pretty easy
      to write a function to do the padding for you:
      >
      #include <stdio.h>
      #include <assert.h>
      #include <limits.h>
      >
      char *pad(char *s, int n, size_t width, int padch)
      {
        int count = 0;
        char *t = s;
        assert(width (sizeof n * CHAR_BIT + 5) / 3);
      What's the purpose of this? Let's say that int is four bytes long. The
      expression on the right side of the '>' will yield 37 / 3 (12). What
      does 'width' represent? (You aren't using it anywhere else in the
      function.)
        count = sprintf(t, "%d", n);
        t += count;
        while(count < len)
      'len' is undefined.
        {
          *t++ = padch;
        }
      And that will yield an infinite loop.
        *t = '\0';
        return s;
      >
      }
      >
      In keeping with the spirit of comp.lang.c, the above is untested (because
      I'm in a hurry).
      Here's a safer and tested version:

      char *PadSnprintf(ch ar *str, // the buffer
      int n, // sizeof(str)
      int item, // the item to convert
      char paddingChar) // the padding character
      {
      snprintf(str, n, "%d", item);

      char *p = str + strlen(str);
      while (p < str + n - 1)
      *p++ = paddingChar;

      *p = '\0';
      return str;
      }

      Sebastian

      Comment

      • Richard Heathfield

        #4
        Re: right padding

        s0suk3@gmail.co m said:
        On Oct 21, 4:17 am, Richard Heathfield <rjh@see.sig.in validwrote:
        <snip>
        >assert(width (sizeof n * CHAR_BIT + 5) / 3);
        >
        What's the purpose of this? Let's say that int is four bytes long. The
        expression on the right side of the '>' will yield 37 / 3 (12).
        No, for four-byte ints it yields *at least* 12. If bytes are wider than
        eight bits, which is legal, it will give a larger value.
        What does 'width' represent?
        It represents the width of the buffer starting at s. It must be big enough
        to hold the text representation of an integer. If int is four octets in
        size, then it could hold a value whose base ten text representation is as
        large as -2147483648, which is eleven bytes long, so the buffer must be at
        least twelve bytes long (one for the null terminator).
        (You aren't using it anywhere else in the function.)
        I did mean to use it...
        >count = sprintf(t, "%d", n);
        >t += count;
        >while(count < len)
        >
        'len' is undefined.
        ....here. I meant width, not len.
        >
        >{
        >*t++ = padch;
        >}
        >
        And that will yield an infinite loop.
        Well, now you're just being picky. :-)


        Here's a safer and tested version:
        >
        char *PadSnprintf(ch ar *str, // the buffer
        int n, // sizeof(str)
        If the comment is correct, why do you need n? Why not just use sizeof(str)?
        But I suspect the comment is wrong.
        int item, // the item to convert
        char paddingChar) // the padding character
        {
        snprintf(str, n, "%d", item);
        Right away, your "safer and tested version" fails to link on a vast number
        of C installations that don't have access to snprintf.

        The two bugs that you spotted in my code (and I thank you for that) are
        trivial to fix. It's rather less trivial to implement snprintf on systems
        that lack it.

        --
        Richard Heathfield <http://www.cpax.org.uk >
        Email: -http://www. +rjh@
        Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
        "Usenet is a strange place" - dmr 29 July 1999

        Comment

        • s0suk3@gmail.com

          #5
          Re: right padding

          On Oct 21, 1:59 pm, Richard Heathfield <rjh@see.sig.in validwrote:
          s0suk3@gmail.co m said:
          On Oct 21, 4:17 am, Richard Heathfield <rjh@see.sig.in validwrote:
          (You aren't using it anywhere else in the function.)
          >
          I did mean to use it...
          >
          count = sprintf(t, "%d", n);
          t += count;
          while(count < len)
          >
          'len' is undefined.
          >
          ...here. I meant width, not len.
          >
          {
          *t++ = padch;
          }
          >
          And that will yield an infinite loop.
          >
          Well, now you're just being picky.  :-)
          A bit :-) Well, even if you would have used 'width' instead of 'len',
          'count' isn't being incremented anywhere.
          Here's a safer and tested version:
          >
          char *PadSnprintf(ch ar *str,         // the buffer
                            int n,             // sizeof(str)
          >
          If the comment is correct, why do you need n? Why not just use sizeof(str)?
          But I suspect the comment is wrong.
          Perhaps the comment is a bit misleading. The confusion is that if you
          have an array declared like 'char str[10];', sizeof(str) will yield
          10, whereas sizeof(str) inside PadSnprintf() will only yield the size
          of a pointer (4 bytes or so), regardless of the size of the array to
          which it points.
                            int item,          // theitem to convert
                            char paddingChar)  // the paddingcharacte r
          {
              snprintf(str, n, "%d", item);
          >
          Right away, your "safer and tested version" fails to link on a vast number
          of C installations that don't have access to snprintf.
          >
          The two bugs that you spotted in my code (and I thank you for that) are
          trivial to fix. It's rather less trivial to implement snprintf on systems
          that lack it.
          Well, I wouldn't call them "a vast number." Even some non-C99
          compilers support it (e.g., MSVC++).

          Sebastian

          Comment

          • Richard Heathfield

            #6
            Re: right padding

            s0suk3@gmail.co m said:
            On Oct 21, 1:59 pm, Richard Heathfield <rjh@see.sig.in validwrote:
            <snip>
            Here's a safer and tested version:
            >>
            char *PadSnprintf(ch ar *str, // the buffer
            int n, // sizeof(str)
            >>
            >If the comment is correct, why do you need n? Why not just use
            >sizeof(str)? But I suspect the comment is wrong.
            >
            Perhaps the comment is a bit misleading. The confusion is that if you
            have an array declared like 'char str[10];', sizeof(str) will yield
            10, whereas sizeof(str) inside PadSnprintf() will only yield the size
            of a pointer (4 bytes or so),
            Or 1, or 2, or whatever. Yes.
            regardless of the size of the array to which it points.
            It doesn't point to an array. It points to the first member of an array.
            int item, // the item to convert
            char paddingChar) // the padding character
            {
            snprintf(str, n, "%d", item);
            >>
            >Right away, your "safer and tested version" fails to link on a vast
            >number of C installations that don't have access to snprintf.
            >>
            >The two bugs that you spotted in my code (and I thank you for that) are
            >trivial to fix. It's rather less trivial to implement snprintf on
            >systems that lack it.
            >
            Well, I wouldn't call them "a vast number."
            I have quite a few implementations installed on various bits of kit around
            here, and only *one* of them provides snprintf (and only when invoked in
            non-conforming mode, and with different semantics to C99).
            Even some non-C99 compilers support it (e.g., MSVC++).
            I checked. My copy doesn't.

            Obviously the call is legal C99. And C99, like the Ritz Hotel, is open to
            all.

            --
            Richard Heathfield <http://www.cpax.org.uk >
            Email: -http://www. +rjh@
            Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
            "Usenet is a strange place" - dmr 29 July 1999

            Comment

            • CBFalconer

              #7
              Re: right padding

              s0suk3@gmail.co m wrote:
              Richard Heathfield <rjh@see.sig.in validwrote:
              >
              .... snip ...
              >
              >If the comment is correct, why do you need n? Why not just use
              >sizeof(str)? But I suspect the comment is wrong.
              >
              Perhaps the comment is a bit misleading. The confusion is that if
              you have an array declared like 'char str[10];', sizeof(str) will
              yield 10, whereas sizeof(str) inside PadSnprintf() will only yield
              the size of a pointer (4 bytes or so), regardless of the size of
              the array to which it points.
              However you can apply the esoteric strlen(s) function to get the
              length of the passed string.

              --
              [mail]: Chuck F (cbfalconer at maineline dot net)
              [page]: <http://cbfalconer.home .att.net>
              Try the download section.

              Comment

              • Ben Bacarisse

                #8
                Re: right padding

                CBFalconer <cbfalconer@yah oo.comwrites:
                s0suk3@gmail.co m wrote:
                >Richard Heathfield <rjh@see.sig.in validwrote:
                >>
                ... snip ...
                >>
                >>If the comment is correct, why do you need n? Why not just use
                >>sizeof(str) ? But I suspect the comment is wrong.
                >>
                >Perhaps the comment is a bit misleading. The confusion is that if
                >you have an array declared like 'char str[10];', sizeof(str) will
                >yield 10, whereas sizeof(str) inside PadSnprintf() will only yield
                >the size of a pointer (4 bytes or so), regardless of the size of
                >the array to which it points.
                >
                However you can apply the esoteric strlen(s) function to get the
                length of the passed string.
                That's unlikely to be useful in this case. The pointer need not point
                at a string -- in fact it would very unusual to require that a
                function of this sort be passed a pointer to string.

                --
                Ben.

                Comment

                • Richard Heathfield

                  #9
                  Re: right padding

                  CBFalconer said:
                  s0suk3@gmail.co m wrote:
                  >Richard Heathfield <rjh@see.sig.in validwrote:
                  >>
                  ... snip ...
                  >>
                  >>If the comment is correct, why do you need n? Why not just use
                  >>sizeof(str) ? But I suspect the comment is wrong.
                  >>
                  >Perhaps the comment is a bit misleading. The confusion is that if
                  >you have an array declared like 'char str[10];', sizeof(str) will
                  >yield 10, whereas sizeof(str) inside PadSnprintf() will only yield
                  >the size of a pointer (4 bytes or so), regardless of the size of
                  >the array to which it points.
                  >
                  However you can apply the esoteric strlen(s) function to get the
                  length of the passed string.
                  What passed string?

                  Has it ever occurred to you to read threads before trying to participate in
                  them?

                  --
                  Richard Heathfield <http://www.cpax.org.uk >
                  Email: -http://www. +rjh@
                  Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                  "Usenet is a strange place" - dmr 29 July 1999

                  Comment

                  • CBFalconer

                    #10
                    Re: right padding

                    Richard Heathfield wrote:
                    CBFalconer said:
                    >s0suk3@gmail.co m wrote:
                    >>Richard Heathfield <rjh@see.sig.in validwrote:
                    >>>
                    >... snip ...
                    >>>
                    >>>If the comment is correct, why do you need n? Why not just use
                    >>>sizeof(str )? But I suspect the comment is wrong.
                    >>>
                    >>Perhaps the comment is a bit misleading. The confusion is that
                    >>if you have an array declared like 'char str[10];', sizeof(str)
                    >>will yield 10, whereas sizeof(str) inside PadSnprintf() will
                    >>only yield the size of a pointer (4 bytes or so), regardless of
                    >>the size of the array to which it points.
                    >>
                    >However you can apply the esoteric strlen(s) function to get the
                    >length of the passed string.
                    >
                    What passed string?
                    >
                    Has it ever occurred to you to read threads before trying to
                    participate in them?
                    The following was the function under discussion. If str is not a
                    string, it should be. Do try to control your love of petty
                    objections.
                    >>>>char *PadSnprintf(ch ar *str, // the buffer
                    --
                    [mail]: Chuck F (cbfalconer at maineline dot net)
                    [page]: <http://cbfalconer.home .att.net>
                    Try the download section.

                    Comment

                    • Richard Heathfield

                      #11
                      Re: right padding

                      CBFalconer said:
                      Richard Heathfield wrote:
                      >CBFalconer said:
                      >>s0suk3@gmail.co m wrote:
                      >>>Richard Heathfield <rjh@see.sig.in validwrote:
                      >>>>
                      >>... snip ...
                      >>>>
                      >>>>If the comment is correct, why do you need n? Why not just use
                      >>>>sizeof(str) ? But I suspect the comment is wrong.
                      >>>>
                      >>>Perhaps the comment is a bit misleading. The confusion is that
                      >>>if you have an array declared like 'char str[10];', sizeof(str)
                      >>>will yield 10, whereas sizeof(str) inside PadSnprintf() will
                      >>>only yield the size of a pointer (4 bytes or so), regardless of
                      >>>the size of the array to which it points.
                      >>>
                      >>However you can apply the esoteric strlen(s) function to get the
                      >>length of the passed string.
                      >>
                      >What passed string?
                      >>
                      >Has it ever occurred to you to read threads before trying to
                      >participate in them?
                      >
                      The following was the function under discussion. If str is not a
                      string, it should be.
                      It isn't, and it shouldn't be. It is *to become* a string. What's wrong
                      with this buggy code that I just buggily wrote to illustrate this point?

                      char iamnotastring[32];
                      iamnotastring[0] = '\0'; /* optional - doesn't affect my point */
                      if(fgets(iamnot astring, strlen(iamnotas tring), stdin) != NULL)
                      {
                      ...
                      Do try to control your love of petty objections.
                      You consider the distinction between a string and an array of char "petty"?
                      >>>>>char *PadSnprintf(ch ar *str, // the buffer
                      str is a pointer to the first character in a buffer that is *going to
                      become* a string. You can't find out how long that buffer is with strlen.
                      You have to be told.

                      And in your case, you have to be told repeatedly.

                      --
                      Richard Heathfield <http://www.cpax.org.uk >
                      Email: -http://www. +rjh@
                      Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                      "Usenet is a strange place" - dmr 29 July 1999

                      Comment

                      • Phil Carmody

                        #12
                        Re: right padding

                        Richard Heathfield <rjh@see.sig.in validwrites:
                        You consider the distinction between a string and an array of char "petty"?
                        Richard, you're hereby invited to the 'Banks and the Economy'
                        thread which I invited to be moved here rather than continue
                        off-topic in alt.lang.asm. (Being about C, rather than asm,
                        or banks.)

                        Phil
                        --
                        The fact that a believer is happier than a sceptic is no more to the
                        point than the fact that a drunken man is happier than a sober one.
                        The happiness of credulity is a cheap and dangerous quality.
                        -- George Bernard Shaw (1856-1950), Preface to Androcles and the Lion

                        Comment

                        • Richard Heathfield

                          #13
                          Re: right padding

                          Phil Carmody said:
                          Richard Heathfield <rjh@see.sig.in validwrites:
                          >You consider the distinction between a string and an array of char
                          >"petty"?
                          >
                          Richard, you're hereby invited to the 'Banks and the Economy'
                          thread which I invited to be moved here rather than continue
                          off-topic in alt.lang.asm. (Being about C, rather than asm,
                          or banks.)
                          It's kind of you, but I had a quick look and I think I'll decline, since
                          it's not obvious what the issue is. (I got as far as discovering that it's
                          about formal parameters, and someone seems to be confused about in what
                          contexts char [] means char [] and in what contexts it means char *.)

                          Why not present the issue as a simple question or statement, in a new
                          thread? Throwing out all the background noise will probably help
                          immensely.

                          --
                          Richard Heathfield <http://www.cpax.org.uk >
                          Email: -http://www. +rjh@
                          Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                          "Usenet is a strange place" - dmr 29 July 1999

                          Comment

                          • Phil Carmody

                            #14
                            Re: right padding

                            Richard Heathfield <rjh@see.sig.in validwrites:
                            Phil Carmody said:
                            >
                            >Richard Heathfield <rjh@see.sig.in validwrites:
                            >>You consider the distinction between a string and an array of char
                            >>"petty"?
                            >>
                            >Richard, you're hereby invited to the 'Banks and the Economy'
                            >thread which I invited to be moved here rather than continue
                            >off-topic in alt.lang.asm. (Being about C, rather than asm,
                            >or banks.)
                            >
                            It's kind of you, but I had a quick look and I think I'll decline, since
                            it's not obvious what the issue is. (I got as far as discovering that it's
                            about formal parameters, and someone seems to be confused about in what
                            contexts char [] means char [] and in what contexts it means char *.)
                            >
                            Why not present the issue as a simple question or statement, in a new
                            thread? Throwing out all the background noise will probably help
                            immensely.
                            It appears that he's admitted he misunderstood C, so it's not just
                            in decline - it's over. Quite why he had to struggle and wriggle
                            so much on a.l.a I don't know, I thought he'd never get the point.
                            It appears that he misunderstood what follow-ups are too.

                            It's nice to know that you're here when needed though!

                            Cheers,
                            Phil
                            (Hi Ian!)
                            --
                            The fact that a believer is happier than a sceptic is no more to the
                            point than the fact that a drunken man is happier than a sober one.
                            The happiness of credulity is a cheap and dangerous quality.
                            -- George Bernard Shaw (1856-1950), Preface to Androcles and the Lion

                            Comment

                            • Ian Collins

                              #15
                              Re: right padding

                              Phil Carmody wrote:
                              >
                              Cheers,
                              Phil
                              (Hi Ian!)
                              G'day Phil!

                              --
                              Ian Collins

                              Comment

                              Working...