add a null to a std::string.

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

    add a null to a std::string.

    I need to make a class called uid.
    A UID is a unique identifier.
    It looks like... 1.2.3.345.1.2.4 .566
    This uid get transmitted over a network as 8 bit binary data.
    If the length of the UID is odd, an extra padding null \0 is added
    to the end.

    This is what I've written but I'm not sure if I've garanteed to have
    the c_str() method return a buffer that is null padded.


    class uid
    {
    private:
    std::string id;
    public:
    uid(std::string _id)
    {
    id = _id;
    unsigned int len = id.size();
    if (len & 0x00000001)
    id.push_back(0x 00);
    }
    int getSize(void)
    {
    return id.size();
    }
    const void *getData(void)
    {
    return id.c_str();
    }
    };

  • Dietmar Kuehl

    #2
    Re: add a null to a std::string.

    JustSomeGuy wrote:[color=blue]
    > This is what I've written but I'm not sure if I've garanteed to have
    > the c_str() method return a buffer that is null padded.[/color]

    The method 'c_str()' will return a pointer to a null-terminated
    representation of the 'std::string's content. That is, you will
    have an extra null-character tagged on to the 'std::string's
    content. If the 'std::string's content contains null-characters,
    you cannot process the result of 'c_str()' with the 'str...()'
    functions because these might stop at the first embedded
    null-character. Thus, you probably should use the 'data()' method
    instead because this has the same effect as 'c_str()' except that
    it does not add an extra null-character.
    --
    <mailto:dietmar _kuehl@yahoo.co m> <http://www.dietmar-kuehl.de/>
    <http://www.eai-systems.com> - Efficient Artificial Intelligence

    Comment

    • roberts.noah@gmail.com

      #3
      Re: add a null to a std::string.


      Dietmar Kuehl wrote:[color=blue]
      > JustSomeGuy wrote:[color=green]
      > > This is what I've written but I'm not sure if I've garanteed to have
      > > the c_str() method return a buffer that is null padded.[/color]
      >
      > The method 'c_str()' will return a pointer to a null-terminated
      > representation of the 'std::string's content. That is, you will
      > have an extra null-character tagged on to the 'std::string's
      > content. If the 'std::string's content contains null-characters,
      > you cannot process the result of 'c_str()' with the 'str...()'
      > functions because these might stop at the first embedded
      > null-character. Thus, you probably should use the 'data()' method
      > instead because this has the same effect as 'c_str()' except that
      > it does not add an extra null-character.[/color]

      Actually c_str would work in this guy's case. He wants to output the 0
      under certain circumstances. So he should be able to just grab c_str
      and output +/- 1 byte depending on conditions.

      Comment

      • JustSomeGuy

        #4
        Re: add a null to a std::string.

        Thank you for your reply.
        Thinking about it I realised that if the getSize method is changed such
        that
        the length returned is rounded up to the nearest even number then the
        data
        returned by getData will included the null, because the c_str method
        points
        to a null terminated string anyways...

        Comment

        • Dietmar Kuehl

          #5
          Re: add a null to a std::string.

          roberts.noah@gm ail.com wrote:[color=blue]
          > Actually c_str would work in this guy's case.[/color]

          I never claimed it doesn't. The way he inserts the null manually,
          he can, however, also use 'data()' ... and it would be clearer
          that the result is not intended for consumption by the 'str...()'
          functions.
          [color=blue]
          > He wants to output the 0
          > under certain circumstances. So he should be able to just grab c_str
          > and output +/- 1 byte depending on conditions.[/color]

          Yes, this would be an approach avoiding the need to manually attach
          the null character.
          --
          <mailto:dietmar _kuehl@yahoo.co m> <http://www.dietmar-kuehl.de/>
          <http://www.eai-systems.com> - Efficient Artificial Intelligence

          Comment

          • loufoque

            #6
            Re: add a null to a std::string.

            Dietmar Kuehl a écrit :[color=blue]
            >
            > Thus, you probably should use the 'data()' method
            > instead because this has the same effect as 'c_str()' except that
            > it does not add an extra null-character.[/color]

            Well actually, data() and c_str() are the same on some implementations
            (like GNU libstdc++)
            Using data() instead of c_str() may prevent a copy though.

            Comment

            • Daniel T.

              #7
              add a null to a std::string.

              In article <468s1qF9ru5hU1 @individual.net >,
              Dietmar Kuehl <dietmar_kuehl@ yahoo.com> wrote:
              [color=blue]
              > JustSomeGuy wrote:[color=green]
              > > This is what I've written but I'm not sure if I've garanteed to have
              > > the c_str() method return a buffer that is null padded.[/color]
              >
              > The method 'c_str()' will return a pointer to a null-terminated
              > representation of the 'std::string's content. That is, you will
              > have an extra null-character tagged on to the 'std::string's
              > content. If the 'std::string's content contains null-characters,
              > you cannot process the result of 'c_str()' with the 'str...()'
              > functions because these might stop at the first embedded
              > null-character. Thus, you probably should use the 'data()' method
              > instead because this has the same effect as 'c_str()' except that
              > it does not add an extra null-character.[/color]

              Let's say I have a string class that doesn't hold its data in a
              contiguous array but still wants to conform to the standard. This would
              mean I have to create an array and fill it whenever c_str is called (and
              I'm free to delete it whenever a non-const member-function is called.)

              Would my class be standard conforming if I only made a c_str array up to
              the first null? (I expect that data would have to have all the
              characters.)

              In other words given:

              int main()
              {
              std::string s = "Hello! World";
              s[6] = 0;
              const char* foo = s.c_str();
              char bar[6];
              std::strcpy( bar, foo + 7 );
              std::cout << bar;
              }

              Does the standard define the output of the above program?


              --
              Magic depends on tradition and belief. It does not welcome observation,
              nor does it profit by experiment. On the other hand, science is based
              on experience; it is open to correction by observation and experiment.

              ---
              [ comp.std.c++ is moderated. To submit articles, try just posting with ]
              [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.e du ]
              [ --- Please see the FAQ before posting. --- ]
              [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

              Comment

              • Kai-Uwe Bux

                #8
                Re: add a null to a std::string.

                Daniel T. wrote:
                [color=blue]
                > In article <468s1qF9ru5hU1 @individual.net >,
                > Dietmar Kuehl <dietmar_kuehl@ yahoo.com> wrote:
                >[color=green]
                >> JustSomeGuy wrote:[color=darkred]
                >> > This is what I've written but I'm not sure if I've garanteed to have
                >> > the c_str() method return a buffer that is null padded.[/color]
                >>
                >> The method 'c_str()' will return a pointer to a null-terminated
                >> representation of the 'std::string's content. That is, you will
                >> have an extra null-character tagged on to the 'std::string's
                >> content. If the 'std::string's content contains null-characters,
                >> you cannot process the result of 'c_str()' with the 'str...()'
                >> functions because these might stop at the first embedded
                >> null-character. Thus, you probably should use the 'data()' method
                >> instead because this has the same effect as 'c_str()' except that
                >> it does not add an extra null-character.[/color]
                >
                > Let's say I have a string class that doesn't hold its data in a
                > contiguous array but still wants to conform to the standard. This would
                > mean I have to create an array and fill it whenever c_str is called (and
                > I'm free to delete it whenever a non-const member-function is called.)
                >
                > Would my class be standard conforming if I only made a c_str array up to
                > the first null? (I expect that data would have to have all the
                > characters.)
                >
                > In other words given:
                >
                > int main()
                > {
                > std::string s = "Hello! World";
                > s[6] = 0;
                > const char* foo = s.c_str();
                > char bar[6];
                > std::strcpy( bar, foo + 7 );
                > std::cout << bar;
                > }
                >
                > Does the standard define the output of the above program?
                >
                >[/color]

                The standard specifies:

                21.3.6/1 const charT* c_str() const;
                Returns: A pointer to the initial element of an array of length size() + 1
                whose first size() elements equal the corresponding elements of the string
                controlled by *this and whose last element is a null character specified by
                charT().


                Thus, you are not allowed to stop at the first 0 char. The size and contents
                of the array pointed to by the return value of c_str() are completely
                specified.


                Best

                Kai-Uwe Bux

                ---
                [ comp.std.c++ is moderated. To submit articles, try just posting with ]
                [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.e du ]
                [ --- Please see the FAQ before posting. --- ]
                [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

                Comment

                Working...