std::string to integer type

Discussion in 'C++' started by Christopher Benson-Manica, May 3, 2004.

  1. Are the C functions atoi() and strtol() (preferred, I'm aware) the
    only way to convert std::string's to integers, or are there other
    alternatives I'm not aware of?

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, May 3, 2004
    #1
    1. Advertising

  2. Christopher Benson-Manica

    Tim Love Guest

    Tim Love, May 3, 2004
    #2
    1. Advertising

  3. Tim Love <> spoke thus:

    > See the FAQ - e.g.
    > http://www.parashift.com/c -faq-lite/misc-technical-issues.html


    Oops!!! I should have known better - sorry for not reading the holy
    writ first :( And unfortunately, I can't use the C++ solution
    either, since stringstreams are hit or miss on my (horribly broken)
    system. Oh well.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, May 3, 2004
    #3
  4. Christopher Benson-Manica

    Karthik Guest

    Christopher Benson-Manica wrote:
    > I can't use the C++ solution
    > either, since stringstreams are hit or miss on my (horribly broken)
    > system. Oh well.


    Do you mean to say, this is not in accordance with the standard (
    and hence, UB ? ) . I am sorry I didnt understand this.

    --
    Karthik.
    Humans please 'removeme_' for my real email.
    Karthik, May 3, 2004
    #4
  5. Karthik <> spoke thus:

    > Do you mean to say, this is not in accordance with the standard (
    > and hence, UB ? ) . I am sorry I didnt understand this.


    No, it means that for a variety of reasons, stringstreams don't behave
    on my system. None of those reasons involve UB - it can be summed up
    succinctly by saying "My system sucks".

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, May 3, 2004
    #5
  6. "Christopher Benson-Manica" <> wrote in message
    news:c75n54$lb5$...
    > Tim Love <> spoke thus:
    >
    > > See the FAQ - e.g.
    > > http://www.parashift.com/c -faq-lite/misc-technical-issues.html

    >
    > Oops!!! I should have known better - sorry for not reading the holy
    > writ first :( And unfortunately, I can't use the C++ solution
    > either, since stringstreams are hit or miss on my (horribly broken)
    > system. Oh well.


    I have two suggestions:

    1. See if you can use lexical_cast from the Boost library:

    http://www.boost.org/libs/conversion/lexical_cast.htm

    It's an elegant solution, and it looks like the author made an attempt to
    work around broken C++ implementations.

    2. I've rewritten the code in the FAQ to use the deprecated strstream
    classes instead of the stringstream classes. Maybe it will work for you:

    #ifndef CONVERT_H
    #define CONVERT_H

    // File: convert.h

    // Based on code from the C++ FAQ Lite:
    // http://www.parashift.com/c -faq-lite/misc-technical-issues.html#faq-38.3
    // Modified to use strstream classes instead of stringstream classes.

    #include <istream>
    #include <ostream>
    #include <stdexcept>
    #include <string>
    #include <strstream>
    #include <typeinfo>

    class BadConversion : public std::runtime_error {
    public:
    BadConversion(const std::string& s)
    : std::runtime_error(s)
    { }
    };

    template<typename T>
    inline std::string stringify(const T& x)
    {
    std::eek:strstream o;
    if (!(o << x << std::ends))
    throw BadConversion(std::string("stringify(")
    + typeid(x).name() + ")");
    std::string s(o.str());
    o.freeze(false);
    return s;
    }

    template<typename T>
    inline void convert(const std::string& s, T& x,
    bool failIfLeftoverChars = true)
    {
    std::istrstream i(s.c_str());
    char c;
    if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
    throw BadConversion(s);
    }

    template<typename T>
    inline T convertTo(const std::string& s,
    bool failIfLeftoverChars = true)
    {
    T x;
    convert(s, x, failIfLeftoverChars);
    return x;
    }

    #endif

    --
    Russell Hanneken

    Remove the 'g' from my address to send me mail.
    Russell Hanneken, May 3, 2004
    #6
    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. Peter Jansson
    Replies:
    5
    Views:
    6,301
    Ivan Vecerina
    Mar 17, 2005
  2. Fei Liu
    Replies:
    9
    Views:
    441
  3. Jeffrey Walton
    Replies:
    10
    Views:
    938
    Mathias Gaunard
    Nov 26, 2006
  4. Martin T.
    Replies:
    7
    Views:
    816
    Martin T.
    Mar 10, 2008
  5. Randy Kramer
    Replies:
    12
    Views:
    374
    Robert Klemme
    Oct 25, 2007
Loading...

Share This Page