typecast int to string

Discussion in 'C++' started by Venkat, Jan 7, 2004.

  1. Venkat

    Venkat Guest

    Hi All,

    I want to typecast int to std::string how can i do it.

    Here is the sample code.

    int NewList[500];

    //Fill the NewList with integers values.
    .......
    .......

    //Replace the file contents with new list values at a specified location.

    int i=0;
    std::string line;
    ifstream inFile(sample);//opens a file to read
    while (getline (inFile, line) )
    {

    int comma1Pos = line.find(',');
    int comma2Pos = line.find(',', comma1Pos+1);

    int numChars = comma2Pos - comma1Pos - 1;

    line.erase(comma1Pos+1, numChars); //
    line.insert(comma1Pos+1, (std::string)NewList);
    i++;
    }
    inFile.close();

    In the above code the function insert takes 2nd argument as a string so i
    was trying to type cast NewList to string.
    The complier throws a type cast error saying type cast int to string is not
    possible.

    Is there any way i can type cast the same.


    regards,
    Venkat
    Venkat, Jan 7, 2004
    #1
    1. Advertising

  2. Try something like

    > int i=0;
    > std::string line;
    > ifstream inFile(sample);//opens a file to read
    > while (getline (inFile, line) )
    > {
    >
    > int comma1Pos = line.find(',');
    > int comma2Pos = line.find(',', comma1Pos+1);
    >
    > int numChars = comma2Pos - comma1Pos - 1;
    >
    > line.erase(comma1Pos+1, numChars); //

    std::eek:stringstream os;
    os << NewList;
    line.insert(comma1Pos+1, os.str());
    > i++;
    > }
    > inFile.close();
    >


    --
    To get my real email adress, remove the two onkas
    --
    Dipl.-Inform. Hendrik Belitz
    Central Institute of Electronics
    Research Center Juelich
    Hendrik Belitz, Jan 7, 2004
    #2
    1. Advertising

  3. On Wed, 07 Jan 2004 19:39:14 +0530, Venkat wrote:

    > Hi All,
    >
    > I want to typecast int to std::string how can i do it.
    >
    > Here is the sample code.
    >
    > int NewList[500];
    >
    > //Fill the NewList with integers values.
    > ......
    > ......
    >
    > //Replace the file contents with new list values at a specified location.
    >
    > int i=0;
    > std::string line;
    > ifstream inFile(sample);//opens a file to read
    > while (getline (inFile, line) )
    > {
    >
    > int comma1Pos = line.find(',');
    > int comma2Pos = line.find(',', comma1Pos+1);
    >
    > int numChars = comma2Pos - comma1Pos - 1;
    >
    > line.erase(comma1Pos+1, numChars); //
    > line.insert(comma1Pos+1, (std::string)NewList);
    > i++;
    > }
    > inFile.close();
    >
    > In the above code the function insert takes 2nd argument as a string so i
    > was trying to type cast NewList to string.
    > The complier throws a type cast error saying type cast int to string is not
    > possible.
    >
    > Is there any way i can type cast the same.


    No, that is not what casting is about. Casting can change something to
    something related. Although for humans integers and their
    string-representations may be related, for computers they are very
    different.

    As a side note, you should never use C-style casts in C++, C++ has much
    better casts: static_cast<>, dynamic_cast<>, const_cast<> and
    reinterpret_cast<>. Familiarize yourself with those and never use the
    C-style casts again. It will save you a lot of grief.

    So the question now becomes, how to convert a number to a string. Thee are
    a number of ways to do so, the easiest and most C++ish:

    #include <sstream>

    std::string toString(int i)
    {
    std::stringstream s;
    s << i;
    return s.c_str();
    }

    OK, this works, but maybe you want to use this for unsigned ints as well.
    Or for longs. You could create the same function multiple times,
    overloading on the argument:

    std::string toString(long) { ... }
    std::string toString(unsigned int) { ... }
    std::string toString(unsigned long) { ... }
    std::string toString(float) { ... }
    std::string toString(double) { ... }

    Fortunately, there is an easier way. We can get the compiler to do it for
    us by using the magic of templates:

    template<typename T>
    std::string toString(T t)
    {
    std::stringstream s;
    s << t;
    return s.c_str();
    }

    This will make the compiler produce all of the above functions
    automagically, but only the ones we actually use! (Do note that this code
    must be 'seen' by the compiler before you use it, you cannot just use a
    prototype and define the function in another C++ file. So this typically
    goes in some header.)

    So if you use toString(i), where i is an integer, the compiler will
    substitute int for T and we end up with exectly the same as above. But if
    we use toString(l), where l is a long, the compiler automagically
    generates the above for a long. Templates can be so incredibly powerful!

    HTH,
    M4
    Martijn Lievaart, Jan 7, 2004
    #3
  4. Venkat

    Evan Carew Guest

    Venkat,

    Probably, you want to try looking at Boost's lexical cast facility. The
    boost designers (a free add on to the STL) have provided save means to
    do what you are asking. so see an example, take a look at

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

    Evan Carew

    Venkat wrote:
    > Hi All,
    >
    > I want to typecast int to std::string how can i do it.
    >
    > Here is the sample code.
    >
    > int NewList[500];
    >
    > //Fill the NewList with integers values.
    > ......
    > ......
    >
    > //Replace the file contents with new list values at a specified location.
    >
    > int i=0;
    > std::string line;
    > ifstream inFile(sample);//opens a file to read
    > while (getline (inFile, line) )
    > {
    >
    > int comma1Pos = line.find(',');
    > int comma2Pos = line.find(',', comma1Pos+1);
    >
    > int numChars = comma2Pos - comma1Pos - 1;
    >
    > line.erase(comma1Pos+1, numChars); //
    > line.insert(comma1Pos+1, (std::string)NewList);
    > i++;
    > }
    > inFile.close();
    >
    > In the above code the function insert takes 2nd argument as a string so i
    > was trying to type cast NewList to string.
    > The complier throws a type cast error saying type cast int to string is not
    > possible.
    >
    > Is there any way i can type cast the same.
    >
    >
    > regards,
    > Venkat
    >
    >
    >
    >
    Evan Carew, Jan 7, 2004
    #4
  5. [Snip]

    > #include <sstream>
    >
    > std::string toString(int i)
    > {
    > std::stringstream s;
    > s << i;
    > return s.c_str();
    > }
    >


    Shouldn't "std::stringstream s" be "std::eek:stringstream s"? Which one is better? Why?

    --The Directive

    > OK, this works, but maybe you want to use this for unsigned ints as well.
    > Or for longs. You could create the same function multiple times,
    > overloading on the argument:
    >
    > std::string toString(long) { ... }
    > std::string toString(unsigned int) { ... }
    > std::string toString(unsigned long) { ... }
    > std::string toString(float) { ... }
    > std::string toString(double) { ... }
    >
    > Fortunately, there is an easier way. We can get the compiler to do it for
    > us by using the magic of templates:
    >
    > template<typename T>
    > std::string toString(T t)
    > {
    > std::stringstream s;
    > s << t;
    > return s.c_str();
    > }
    >
    > This will make the compiler produce all of the above functions
    > automagically, but only the ones we actually use! (Do note that this code
    > must be 'seen' by the compiler before you use it, you cannot just use a
    > prototype and define the function in another C++ file. So this typically
    > goes in some header.)
    >
    > So if you use toString(i), where i is an integer, the compiler will
    > substitute int for T and we end up with exectly the same as above. But if
    > we use toString(l), where l is a long, the compiler automagically
    > generates the above for a long. Templates can be so incredibly powerful!
    >
    > HTH,
    > M4
    The Directive, Jan 8, 2004
    #5
  6. On Wed, 07 Jan 2004 18:37:07 -0800, The Directive wrote:

    > [Snip]
    >
    >> #include <sstream>
    >>
    >> std::string toString(int i)
    >> {
    >> std::stringstream s;
    >> s << i;
    >> return s.c_str();
    >> }
    >>
    >>

    > Shouldn't "std::stringstream s" be "std::eek:stringstream s"? Which one is
    > better? Why?


    Yes.. Somehow I have a problem remembering which stringstream to use,
    while I now find it obvious. I thought about it, but did not want any risk
    of getting it wrong. Stupid of me really.

    M4
    Martijn Lievaart, Jan 8, 2004
    #6
  7. Venkat

    Rolf Magnus Guest

    Martijn Lievaart wrote:

    > On Wed, 07 Jan 2004 18:37:07 -0800, The Directive wrote:
    >
    >> [Snip]
    >>
    >>> #include <sstream>
    >>>
    >>> std::string toString(int i)
    >>> {
    >>> std::stringstream s;
    >>> s << i;
    >>> return s.c_str();
    >>> }
    >>>
    >>>

    >> Shouldn't "std::stringstream s" be "std::eek:stringstream s"? Which one
    >> is better? Why?

    >
    > Yes.. Somehow I have a problem remembering which stringstream to use,
    > while I now find it obvious. I thought about it, but did not want any
    > risk of getting it wrong. Stupid of me really.


    I was always wondering why there is an istringstream and an
    ostringstream if a stringstream already does what both can do. So
    what's the actual advantage of using an ostringstream over a
    stringstream?
    Rolf Magnus, Jan 8, 2004
    #7
  8. On Thu, 08 Jan 2004 10:48:40 +0100, Rolf Magnus wrote:

    > I was always wondering why there is an istringstream and an
    > ostringstream if a stringstream already does what both can do. So
    > what's the actual advantage of using an ostringstream over a
    > stringstream?


    I'm guessing, efficiency. An istream and an ostream both need to maintain
    state, so you'll shave of a few bytes and operations by using the best
    suitable class.

    Also, on implementations that link complete objects (I guess most
    implementations do this), as opposed to linking only the parts of an
    object you need, you might link in much more than you actually need.

    HTH,
    M4
    Martijn Lievaart, Jan 8, 2004
    #8
  9. Venkat

    Gavin Deane Guest

    Martijn Lievaart <> wrote in message news:<>...
    > On Wed, 07 Jan 2004 19:39:14 +0530, Venkat wrote:


    <snip>

    > template<typename T>
    > std::string toString(T t)
    > {
    > std::stringstream s;
    > s << t;
    > return s.c_str();


    ITYM
    return s.str();

    > }


    --
    GJD
    Gavin Deane, Jan 8, 2004
    #9
  10. On Thu, 08 Jan 2004 04:26:12 -0800, Gavin Deane wrote:

    > Martijn Lievaart <> wrote in message
    > news:<>...
    >> On Wed, 07 Jan 2004 19:39:14 +0530, Venkat wrote:

    >
    > <snip>
    >
    >> template<typename T>
    >> std::string toString(T t)
    >> {
    >> std::stringstream s;
    >> s << t;
    >> return s.c_str();

    >
    > ITYM
    > return s.str();
    >
    >> }


    Yup, thanks.

    M4
    Martijn Lievaart, Jan 8, 2004
    #10
    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. Schnoffos
    Replies:
    2
    Views:
    1,206
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,626
    Old Wolf
    Jan 20, 2004
  3. arun
    Replies:
    8
    Views:
    450
    Dave Thompson
    Jul 31, 2006
  4. Ulrich Hobelmann

    Typecast illegal from int to pointer?

    Ulrich Hobelmann, Jun 4, 2006, in forum: C++
    Replies:
    2
    Views:
    341
    Ulrich Hobelmann
    Jun 4, 2006
  5. Replies:
    3
    Views:
    975
    Rolf Magnus
    Feb 5, 2007
Loading...

Share This Page