Double -> C-String conversion

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

    Double -> C-String conversion

    During every iteration in a loop, I need to convert a double value into a
    char*. Simple type-casting did not work, so I thought of using
    stringstreams. However, the stream would have to be *emptied* after each
    iteration (and I could not conceive of a practical way doing that).

    Cheers,
    Matthias
    --
    Für emails Anweisung in der Adresse befolgen


  • Rolf Magnus

    #2
    Re: Double -> C-String conversion

    Der Andere wrote:
    [color=blue]
    > During every iteration in a loop, I need to convert a double value
    > into a char*. Simple type-casting did not work, so I thought of using
    > stringstreams.[/color]

    Sounds as if you want a string representation of your double. A char* is
    not a string. It's a pointer to char, nothing less, nothing more.
    [color=blue]
    > However, the stream would have to be *emptied* after each iteration
    > (and I could not conceive of a practical way doing that).[/color]

    Do the following:

    thestream.str(" ");

    Comment

    • Karthik

      #3
      Re: Double -> C-String conversion

      Der Andere wrote:
      [color=blue]
      > During every iteration in a loop, I need to convert a double value into a
      > char*. Simple type-casting did not work, so I thought of using
      > stringstreams. However, the stream would have to be *emptied* after each
      > iteration (and I could not conceive of a practical way doing that).[/color]

      How about using sprintf ?

      sprintf(p, "%lf", doubleval);
      // Remember to allocate memory to p before you do this.

      HTH[color=blue]
      >
      > Cheers,
      > Matthias
      > --
      > Für emails Anweisung in der Adresse befolgen
      >
      >[/color]


      --
      Karthik

      ------

      Human Beings please 'removeme' for my email.

      Comment

      • Jeff Schwab

        #4
        Re: Double -> C-String conversion

        Rolf Magnus wrote:[color=blue]
        > Der Andere wrote:
        >
        >[color=green]
        >>During every iteration in a loop, I need to convert a double value
        >>into a char*. Simple type-casting did not work, so I thought of using
        >>stringstreams .[/color]
        >
        >
        > Sounds as if you want a string representation of your double. A char* is
        > not a string. It's a pointer to char, nothing less, nothing more.
        >
        >[color=green]
        >>However, the stream would have to be *emptied* after each iteration
        >>(and I could not conceive of a practical way doing that).[/color]
        >
        >
        > Do the following:
        >
        > thestream.str(" ");[/color]

        Alternatively:

        thestream.str( ).clear( );

        Comment

        • Der Andere

          #5
          Re: Double -> C-String conversion

          > > During every iteration in a loop, I need to convert a double value[color=blue][color=green]
          > > into a char*. Simple type-casting did not work, so I thought of using
          > > stringstreams.[/color]
          >
          > Sounds as if you want a string representation of your double. A char* is
          > not a string. It's a pointer to char, nothing less, nothing more.[/color]

          C-string, yes. I thought I could indicate a c-string with char *: Would
          char[] be better?
          [color=blue][color=green]
          > > However, the stream would have to be *emptied* after each iteration
          > > (and I could not conceive of a practical way doing that).[/color]
          >
          > Do the following:
          >
          > thestream.str(" ");[/color]

          It works now, cheers :-)

          Matthias


          Comment

          • Rolf Magnus

            #6
            Re: Double -> C-String conversion

            Jeff Schwab wrote:
            [color=blue]
            > Rolf Magnus wrote:[color=green]
            >> Der Andere wrote:
            >>
            >>[color=darkred]
            >>>During every iteration in a loop, I need to convert a double value
            >>>into a char*. Simple type-casting did not work, so I thought of using
            >>>stringstream s.[/color]
            >>
            >>
            >> Sounds as if you want a string representation of your double. A char*
            >> is not a string. It's a pointer to char, nothing less, nothing more.
            >>
            >>[color=darkred]
            >>>However, the stream would have to be *emptied* after each iteration
            >>>(and I could not conceive of a practical way doing that).[/color]
            >>
            >>
            >> Do the following:
            >>
            >> thestream.str(" ");[/color]
            >
            > Alternatively:
            >
            > thestream.str( ).clear( );[/color]

            According to Stroustrup, this shouldn't empty the stream, because str()
            returns a copy of the string.

            Comment

            • Rolf Magnus

              #7
              Re: Double -> C-String conversion

              Der Andere wrote:
              [color=blue][color=green][color=darkred]
              >> > During every iteration in a loop, I need to convert a double value
              >> > into a char*. Simple type-casting did not work, so I thought of
              >> > using stringstreams.[/color]
              >>
              >> Sounds as if you want a string representation of your double. A char*
              >> is not a string. It's a pointer to char, nothing less, nothing more.[/color]
              >
              > C-string, yes. I thought I could indicate a c-string with char *:
              > Would char[] be better?[/color]

              No. Actually, char[] is only allowed if used as a paremter. I just
              wanted to clarify that char* isn't C's string type. C doesn't have a
              string type. Therefore, there is no built-in way to convert something
              into a string, and that explains why casting to char* doesn't do what
              you want.

              Comment

              • Jack Klein

                #8
                Re: Double -> C-String conversion

                On Sun, 25 Apr 2004 16:37:02 -0700, Karthik
                <removeme_kayka ydreamz@yahoo.c om> wrote in comp.lang.c++:
                [color=blue]
                > Der Andere wrote:
                >[color=green]
                > > During every iteration in a loop, I need to convert a double value into a
                > > char*. Simple type-casting did not work, so I thought of using
                > > stringstreams. However, the stream would have to be *emptied* after each
                > > iteration (and I could not conceive of a practical way doing that).[/color]
                >
                > How about using sprintf ?[/color]

                That's a possibility but...
                [color=blue]
                > sprintf(p, "%lf", doubleval);[/color]

                In all versions of the ISO C standard prior to 1999, including the
                1995 version that the C++ standard inherits from, "%lf" is an
                ill-formed conversion specifier for the *printf() functions and
                produces undefined behavior.

                There is no need for separate conversion specifiers for float and
                double in *printf, as floats are always promoted to double as is the
                case for all optional parameters to all variadic functions in C and
                C++.

                The 1999 update to the C standard made the 'l' length modifier a no-op
                with the all floating point conversion specifiers, but this is not
                part of the current C++ standard. The "%lf" *printf conversion
                specifier causes undefined behavior in C++.
                [color=blue]
                > // Remember to allocate memory to p before you do this.
                >
                > HTH[color=green]
                > >
                > > Cheers,
                > > Matthias[/color][/color]

                --
                Jack Klein
                Home: http://JK-Technology.Com
                FAQs for
                comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
                comp.lang.c++ http://www.parashift.com/c++-faq-lite/
                alt.comp.lang.l earn.c-c++

                Comment

                • John Harrison

                  #9
                  Re: Double -&gt; C-String conversion


                  "Jeff Schwab" <jeffplus@comca st.net> wrote in message
                  news:3oadncIDba TV0xHdRVn-uQ@comcast.com. ..[color=blue]
                  > Rolf Magnus wrote:[color=green]
                  > > Der Andere wrote:
                  > >
                  > >[color=darkred]
                  > >>During every iteration in a loop, I need to convert a double value
                  > >>into a char*. Simple type-casting did not work, so I thought of using
                  > >>stringstreams .[/color]
                  > >
                  > >
                  > > Sounds as if you want a string representation of your double. A char* is
                  > > not a string. It's a pointer to char, nothing less, nothing more.
                  > >
                  > >[color=darkred]
                  > >>However, the stream would have to be *emptied* after each iteration
                  > >>(and I could not conceive of a practical way doing that).[/color]
                  > >
                  > >
                  > > Do the following:
                  > >
                  > > thestream.str(" ");[/color]
                  >
                  > Alternatively:
                  >
                  > thestream.str( ).clear( );[/color]

                  That doesn't work. It clears the error flags of the stream. That's what
                  clear does for any stream, it has nothing to do with emptying a string
                  stream.

                  john


                  Comment

                  • John Harrison

                    #10
                    Re: Double -&gt; C-String conversion


                    "Jack Klein" <jackklein@spam cop.net> wrote in message
                    news:sg1p80p2m8 hkber2rph0gqvoe 7aqhvaqpg@4ax.c om...[color=blue]
                    > On Sun, 25 Apr 2004 16:37:02 -0700, Karthik
                    > <removeme_kayka ydreamz@yahoo.c om> wrote in comp.lang.c++:
                    >[color=green]
                    > > Der Andere wrote:
                    > >[color=darkred]
                    > > > During every iteration in a loop, I need to convert a double value[/color][/color][/color]
                    into a[color=blue][color=green][color=darkred]
                    > > > char*. Simple type-casting did not work, so I thought of using
                    > > > stringstreams. However, the stream would have to be *emptied* after[/color][/color][/color]
                    each[color=blue][color=green][color=darkred]
                    > > > iteration (and I could not conceive of a practical way doing that).[/color]
                    > >
                    > > How about using sprintf ?[/color]
                    >
                    > That's a possibility but...
                    >[color=green]
                    > > sprintf(p, "%lf", doubleval);[/color]
                    >
                    > In all versions of the ISO C standard prior to 1999, including the
                    > 1995 version that the C++ standard inherits from, "%lf" is an
                    > ill-formed conversion specifier for the *printf() functions and
                    > produces undefined behavior.
                    >[/color]

                    How did this misunderstandin g become so widespread? So widespread in fact
                    that the C standard committee felt obliged to change the standard.

                    john


                    Comment

                    • Brian Rodenborn

                      #11
                      Re: Double -&gt; C-String conversion


                      Rolf Magnus <ramagnus@t-online.de> wrote in message
                      news:c6hld1$e3j $05$2@news.t-online.com...
                      [color=blue]
                      > No. Actually, char[] is only allowed if used as a paremter. I just
                      > wanted to clarify that char* isn't C's string type. C doesn't have a
                      > string type. Therefore, there is no built-in way to convert something
                      > into a string, and that explains why casting to char* doesn't do what
                      > you want.[/color]


                      Nor does C++ have a string type. It has a library template instantiation
                      called. C has a string concept, it is not an intrinsic type just as C++'s is
                      not. That C string, and you will find the term defined in the C standard, is
                      an array or array-like region of char terminated by a null character.

                      To convert a double to such a string, the sprintf() function can be used. I
                      would recommend using C++ strstreams instead.


                      Brian Rodenborn


                      Comment

                      • Ivan Vecerina

                        #12
                        Re: Double -&gt; C-String conversion

                        "John Harrison" <john_andronicu s@hotmail.com> wrote in message
                        news:c6i5q4$c6l s2$1@ID-196037.news.uni-berlin.de...[color=blue][color=green][color=darkred]
                        > > > sprintf(p, "%lf", doubleval);[/color]
                        > >
                        > > In all versions of the ISO C standard prior to 1999, including the
                        > > 1995 version that the C++ standard inherits from, "%lf" is an
                        > > ill-formed conversion specifier for the *printf() functions and
                        > > produces undefined behavior.[/color]
                        >
                        > How did this misunderstandin g become so widespread? So widespread in fact
                        > that the C standard committee felt obliged to change the standard.[/color]

                        Because when you want to read a double value, you use:
                        sscanf(p, "%lf", &doubleval); // for float: ... "%f", &floatval)

                        The implicit conversion of variadic function parameters from
                        float to double does not apply to pointers, as used in scan functions.

                        Given that the implicit conversion exists in printf (the parameters
                        received are always double, never float), it kind of makes sense
                        that %f and %lf be made equivalent, or even that %lf be the primary
                        choice. (But as Jack pointed out, %lf is formally UB in C++98 and C90).


                        Regards,
                        Ivan
                        --
                        http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form


                        Comment

                        • Jeff Schwab

                          #13
                          Re: Double -&gt; C-String conversion

                          Rolf Magnus wrote:[color=blue]
                          > Jeff Schwab wrote:
                          >
                          >[color=green]
                          >>Rolf Magnus wrote:
                          >>[color=darkred]
                          >>>Der Andere wrote:
                          >>>
                          >>>
                          >>>
                          >>>>During every iteration in a loop, I need to convert a double value
                          >>>>into a char*. Simple type-casting did not work, so I thought of using
                          >>>>stringstrea ms.
                          >>>
                          >>>
                          >>>Sounds as if you want a string representation of your double. A char*
                          >>>is not a string. It's a pointer to char, nothing less, nothing more.
                          >>>
                          >>>
                          >>>
                          >>>>However, the stream would have to be *emptied* after each iteration
                          >>>>(and I could not conceive of a practical way doing that).
                          >>>
                          >>>
                          >>>Do the following:
                          >>>
                          >>>thestream.st r("");[/color]
                          >>
                          >>Alternatively :
                          >>
                          >> thestream.str( ).clear( );[/color]
                          >
                          >
                          > According to Stroustrup, this shouldn't empty the stream, because str()
                          > returns a copy of the string.[/color]

                          Whoops! You're absolutely right.

                          Comment

                          • Jeff Schwab

                            #14
                            Re: Double -&gt; C-String conversion

                            John Harrison wrote:[color=blue]
                            > "Jeff Schwab" <jeffplus@comca st.net> wrote in message
                            > news:3oadncIDba TV0xHdRVn-uQ@comcast.com. ..
                            >[color=green]
                            >>Rolf Magnus wrote:
                            >>[color=darkred]
                            >>>Der Andere wrote:
                            >>>
                            >>>
                            >>>
                            >>>>During every iteration in a loop, I need to convert a double value
                            >>>>into a char*. Simple type-casting did not work, so I thought of using
                            >>>>stringstrea ms.
                            >>>
                            >>>
                            >>>Sounds as if you want a string representation of your double. A char* is
                            >>>not a string. It's a pointer to char, nothing less, nothing more.
                            >>>
                            >>>
                            >>>
                            >>>>However, the stream would have to be *emptied* after each iteration
                            >>>>(and I could not conceive of a practical way doing that).
                            >>>
                            >>>
                            >>>Do the following:
                            >>>
                            >>>thestream.st r("");[/color]
                            >>
                            >>Alternatively :
                            >>
                            >> thestream.str( ).clear( );[/color]
                            >
                            >
                            > That doesn't work. It clears the error flags of the stream. That's what
                            > clear does for any stream, it has nothing to do with emptying a string
                            > stream.[/color]

                            No, John, you've mistaken the clear() method of the stream for the
                            clear() method of the underlying stream. The str() method here returns
                            a string, not a stream.

                            This is a great example of why unnecessary abbreviations are such a bad
                            idea when choosing identifiers.

                            Comment

                            • Jeff Schwab

                              #15
                              Re: Double -&gt; C-String conversion

                              John Harrison wrote:[color=blue]
                              > "Jeff Schwab" <jeffplus@comca st.net> wrote in message
                              > news:3oadncIDba TV0xHdRVn-uQ@comcast.com. ..
                              >[color=green]
                              >>Rolf Magnus wrote:
                              >>[color=darkred]
                              >>>Der Andere wrote:
                              >>>
                              >>>
                              >>>
                              >>>>During every iteration in a loop, I need to convert a double value
                              >>>>into a char*. Simple type-casting did not work, so I thought of using
                              >>>>stringstrea ms.
                              >>>
                              >>>
                              >>>Sounds as if you want a string representation of your double. A char* is
                              >>>not a string. It's a pointer to char, nothing less, nothing more.
                              >>>
                              >>>
                              >>>
                              >>>>However, the stream would have to be *emptied* after each iteration
                              >>>>(and I could not conceive of a practical way doing that).
                              >>>
                              >>>
                              >>>Do the following:
                              >>>
                              >>>thestream.st r("");[/color]
                              >>
                              >>Alternatively :
                              >>
                              >> thestream.str( ).clear( );[/color]
                              >
                              >
                              > That doesn't work. It clears the error flags of the stream. That's what
                              > clear does for any stream, it has nothing to do with emptying a string
                              > stream.
                              >
                              > john
                              >
                              >[/color]

                              No, John, you're wrong. You've mistaken the clear() method of the
                              stream for the clear() method of the underlying stream. The str()
                              method here returns a string, not a stream. This does clear the string
                              representation of the stream's contents, not the stream's error flags.
                              (Unfortunately, as Rolf pointed out, it clears only a temporary copy of
                              the string, so the stream is unaffected. I wonder, why isnt the
                              temporary string const?)

                              This is a great example of why unnecessary abbreviations are such a bad
                              idea when choosing identifiers.

                              Comment

                              Working...