Convert a string to float and vice versa

Discussion in 'C++' started by Flyingaway, Feb 19, 2005.

  1. Flyingaway

    Flyingaway Guest

    I am just wondering what is the most elegant and efficient way to do
    such type conversion.

    Thanks,

    Flyingway
     
    Flyingaway, Feb 19, 2005
    #1
    1. Advertising

  2. "Flyingaway" <> wrote in message
    news:...
    >I am just wondering what is the most elegant and efficient way to do
    > such type conversion.

    Consider using boost::lexical_cast
    See: http://www.boost.org/libs/conversion/lexical_cast.htm

    For maximum efficiency, one could use a statically allocated
    buffer and the functions atof (in <cstdlib>) and ftoa(when
    available, otherwise sprintf). But for the latter you need
    to be very careful with buffer overflows - using snprintf
    may help...


    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
     
    Ivan Vecerina, Feb 19, 2005
    #2
    1. Advertising

  3. Flyingaway

    Flyingaway Guest

    Hi Ivan,

    That is so helpful! You are greatly appreciated. Thanks.

    Best wishes,

    Flying...
     
    Flyingaway, Feb 19, 2005
    #3
  4. Flyingaway

    Stu Guest

    Ivan Vecerina wrote:

    > "Flyingaway" <> wrote in message
    > news:...
    >>I am just wondering what is the most elegant and efficient way to do
    >> such type conversion.

    > Consider using boost::lexical_cast
    > See: http://www.boost.org/libs/conversion/lexical_cast.htm
    >
    > For maximum efficiency, one could use a statically allocated
    > buffer and the functions atof (in <cstdlib>) and ftoa(when
    > available, otherwise sprintf). But for the latter you need
    > to be very careful with buffer overflows - using snprintf
    > may help...


    Why not just use a stringstream? Something like:

    #include <sstream>

    ....

    std::stringstream ss;

    ....

    // Cast to float...
    ss << some_string;
    ss >> some_float;

    ....

    // Cast to string...
    ss >> some_float;
    ss << some_string;

    ....


    No third party library or C functions needed...


    Stu
     
    Stu, Feb 19, 2005
    #4
  5. Flyingaway

    Stu Guest

    Stu wrote:

    > // Cast to string...
    > ss >> some_float;
    > ss << some_string;


    Whoops, copy and paste error! It should look like this:

    // Cast to string...
    ss << some_float;
    ss >> some_string;


    Stu
     
    Stu, Feb 19, 2005
    #5
  6. "Stu" <> wrote in message
    news:...
    > Ivan Vecerina wrote:
    >
    >> "Flyingaway" <> wrote in message
    >> news:...
    >>>I am just wondering what is the most elegant and efficient way to do
    >>> such type conversion.

    >> Consider using boost::lexical_cast
    >> See: http://www.boost.org/libs/conversion/lexical_cast.htm

    Seemed to be the most elegant.

    >> For maximum efficiency, one could use a statically allocated
    >> buffer and the functions atof (in <cstdlib>) and ftoa(when
    >> available, otherwise sprintf). But for the latter you need
    >> to be very careful with buffer overflows - using snprintf
    >> may help...

    That was for the most efficient.

    > Why not just use a stringstream? Something like:


    I totally agree it's a good solution (and it is actually
    boost::lexical_cast uses by default). I had narrowly
    answered the question that was posted...

    Cheers,
    Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
     
    Ivan Vecerina, Feb 19, 2005
    #6
  7. Flyingaway

    Jack Klein Guest

    On Sat, 19 Feb 2005 08:59:05 +0100, "Ivan Vecerina"
    <> wrote in comp.lang.c++:

    > "Flyingaway" <> wrote in message
    > news:...
    > >I am just wondering what is the most elegant and efficient way to do
    > > such type conversion.

    > Consider using boost::lexical_cast
    > See: http://www.boost.org/libs/conversion/lexical_cast.htm
    >
    > For maximum efficiency, one could use a statically allocated
    > buffer and the functions atof (in <cstdlib>) and ftoa(when
    > available, otherwise sprintf). But for the latter you need
    > to be very careful with buffer overflows - using snprintf
    > may help...


    Poor advice, atof and its siblings produce undefined behavior if the
    converted value is outside the range of the type.

    That is specifically why the strtod and siblings were invented.

    --
    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.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Feb 19, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -
    Replies:
    8
    Views:
    621
    Antti S. Brax
    Jun 11, 2005
  2. bd
    Replies:
    0
    Views:
    642
  3. Byron
    Replies:
    6
    Views:
    504
    Byron
    Sep 16, 2004
  4. thinktwice
    Replies:
    4
    Views:
    907
    =?iso-8859-1?q?Kirit_S=E6lensminde?=
    Sep 7, 2006
  5. Replies:
    3
    Views:
    8,817
Loading...

Share This Page