string to integer

Discussion in 'C++' started by Steve, May 24, 2004.

  1. Steve

    Steve Guest

    Hi,

    How can I convert a C++ string to an integer and then an integer back to
    a string? Is there built-in support for this?

    Thanks,

    Steve
    Steve, May 24, 2004
    #1
    1. Advertising

  2. "Steve" <nospam@nopes> wrote in message
    news:40b19a93$...
    > Hi,
    >
    > How can I convert a C++ string to an integer and then an integer back to
    > a string? Is there built-in support for this?
    >
    > Thanks,
    >


    Yes lots of support. But what do you mean by string? For C style strings you
    can use sprintf (int to string) or sscanf (string to int). For C++ strings
    you use stringstream classes.

    #include <sstream>
    #include <string>
    using namespace std;

    // string to int
    string some_string;
    istringstream buffer(some_string);
    int some_int;
    buffer >> some_int;

    // int to string
    int some_int;
    ostringstream buffer;
    buffer << some_int;
    string some_string = buffer.str();

    You might notice that string streams use exactly the same reading and
    writing methods as other streams (cin, cout etc). This is not a coincidence,
    and makes string streams useful for a lot of things besides converting
    strings to integers.

    john
    John Harrison, May 24, 2004
    #2
    1. Advertising

  3. Steve <nospam@nopes> wrote:
    > How can I convert a C++ string to an integer and then an integer back to
    > a string? Is there built-in support for this?


    You can either call the functions atoi and itoa declared in stdlib or use
    stringstreams for conversion:

    int str2int (const string &str) {
    stringstream ss(str);
    int n;
    ss >> n;
    return n;
    }

    string int2str (int n) {
    stringstream ss;
    ss << n;
    return ss.str();
    }

    Another possibility is the lexical_cast template from the boost libraries
    (www.boost.org).

    Martin
    Martin Gieseking, May 24, 2004
    #3
  4. Steve

    Sumit Rajan Guest

    Sumit Rajan, May 24, 2004
    #4
  5. Steve

    Alan Johnson Guest

    Martin Gieseking wrote:
    > Steve <nospam@nopes> wrote:
    >
    >>How can I convert a C++ string to an integer and then an integer back to
    >>a string? Is there built-in support for this?

    >
    >
    > You can either call the functions atoi and itoa declared in stdlib or use
    > stringstreams for conversion:
    >
    > int str2int (const string &str) {
    > stringstream ss(str);
    > int n;
    > ss >> n;
    > return n;
    > }
    >
    > string int2str (int n) {
    > stringstream ss;
    > ss << n;
    > return ss.str();
    > }
    >
    > Another possibility is the lexical_cast template from the boost libraries
    > (www.boost.org).
    >
    > Martin


    To nitpick, the C++ standard doesn't define any function called itoa.
    Use sprintf if you want to work with C-style strings and care about
    portability.

    Alan
    Alan Johnson, May 24, 2004
    #5
  6. Steve

    Steve Guest

    John Harrison wrote:
    > "Steve" <nospam@nopes> wrote in message
    > news:40b19a93$...
    >
    >>Hi,
    >>
    >>How can I convert a C++ string to an integer and then an integer back to
    >>a string? Is there built-in support for this?
    >>
    >>Thanks,
    >>

    >
    >
    > Yes lots of support. But what do you mean by string? For C style strings you
    > can use sprintf (int to string) or sscanf (string to int). For C++ strings
    > you use stringstream classes.
    >
    > #include <sstream>
    > #include <string>
    > using namespace std;
    >
    > // string to int
    > string some_string;
    > istringstream buffer(some_string);
    > int some_int;
    > buffer >> some_int;
    >
    > // int to string
    > int some_int;
    > ostringstream buffer;
    > buffer << some_int;
    > string some_string = buffer.str();
    >
    > You might notice that string streams use exactly the same reading and
    > writing methods as other streams (cin, cout etc). This is not a coincidence,
    > and makes string streams useful for a lot of things besides converting
    > strings to integers.
    >
    > john
    >
    >



    Thanks guys, this helped a lot! :)

    Cheers,

    Steve

    ------------ And now a word from our sponsor ---------------------
    For a secure high performance FTP using SSL/TLS encryption
    upgrade to SurgeFTP
    ---- See http://netwinsite.com/sponsor/sponsor_surgeftp.htm ----
    Steve, May 27, 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. =?Utf-8?B?Sm9l?=

    CType(x,Integer) vs. Integer.Parse(x)

    =?Utf-8?B?Sm9l?=, Feb 6, 2006, in forum: ASP .Net
    Replies:
    7
    Views:
    5,938
    =?Utf-8?B?RGF2aWQgQW50b24=?=
    Feb 7, 2006
  2. =?ISO-8859-1?Q?Thomas_Gagn=E9?=

    No Math.min(Integer, Integer)?

    =?ISO-8859-1?Q?Thomas_Gagn=E9?=, Jul 29, 2003, in forum: Java
    Replies:
    0
    Views:
    492
    =?ISO-8859-1?Q?Thomas_Gagn=E9?=
    Jul 29, 2003
  3. Sebastian Stelzer

    How do I add an Integer to another Integer?

    Sebastian Stelzer, Oct 14, 2004, in forum: Java
    Replies:
    2
    Views:
    480
    Yu SONG
    Oct 15, 2004
  4. Sebastian Stelzer

    How do I add an Integer to another Integer?

    Sebastian Stelzer, Oct 14, 2004, in forum: Java
    Replies:
    6
    Views:
    45,347
    JavaBean2010
    Apr 7, 2010
  5. Randy Kramer
    Replies:
    12
    Views:
    359
    Robert Klemme
    Oct 25, 2007
Loading...

Share This Page