convert integer to string

Discussion in 'C++' started by compboy, Sep 10, 2006.

  1. compboy

    compboy Guest

    Can anyone help me about this.

    I have been trying all the ways I knew and I could find but just didnt
    work.

    I have tried:

    using itoa but it says that it doesnt have that function.

    and also

    String aa = 34;

    cout << aa.val() <endl;

    but again just didnt work out it even cant find the String as a
    declaration.
    I tried to change it into string but still didnt work

    and I tried to do it like c:

    aa = 30+" ";
    and still didnt work.

    just for information that I have included all the libraries I need for
    those functions mentioned above.

    Thanks a lot.
     
    compboy, Sep 10, 2006
    #1
    1. Advertising

  2. compboy

    Tim Love Guest

    "compboy" <> writes:

    >Can anyone help me about this.

    See the FAQ - http://www.parashift.com/c -faq-lite/misc-technical-issues.html
    - which has a complete example.


    >just for information that I have included all the libraries I need for
    >those functions mentioned above.

    Next time it would be useful to include a complete example.
     
    Tim Love, Sep 10, 2006
    #2
    1. Advertising

  3. compboy wrote:
    > Can anyone help me about this.
    >
    > I have been trying all the ways I knew and I could find but just didnt
    > work.
    >
    > I have tried:
    >
    > using itoa but it says that it doesnt have that function.
    >
    > and also
    >
    > String aa = 34;
    >
    > cout << aa.val() <endl;
    >
    > but again just didnt work out it even cant find the String as a
    > declaration.
    > I tried to change it into string but still didnt work
    >
    > and I tried to do it like c:
    >
    > aa = 30+" ";
    > and still didnt work.


    I have never seen an error message that said "didn't work."

    Try reporting an actual error message. You can get better help by
    providing better information.

    --
    Scott McPhillips [VC++ MVP]
     
    Scott McPhillips [MVP], Sep 10, 2006
    #3
  4. compboy

    osmium Guest

    "compboy" writes:

    > I have been trying all the ways I knew and I could find but just didnt
    > work.
    >
    > I have tried:
    >
    > using itoa but it says that it doesnt have that function.
    >
    > and also
    >
    > String aa = 34;


    String is just an ordinary identifier in C++. string has a special meaning
    depending on includes and using statements.

    > cout << aa.val() <endl;
    >
    > but again just didnt work out it even cant find the String as a
    > declaration.
    > I tried to change it into string but still didnt work
    >
    > and I tried to do it like c:
    >
    > aa = 30+" ";
    > and still didnt work.
    >
    > just for information that I have included all the libraries I need for
    > those functions mentioned above.


    The easiest way may be to use sscanf() in <cstdio>. A purist would probably
    use something in <iostream>, it's been a while since I did this, from
    memory, perhaps istrstream
     
    osmium, Sep 10, 2006
    #4
  5. compboy

    benben Guest

    [snip]
    >>
    >> String aa = 34;

    >
    > String is just an ordinary identifier in C++. string has a special meaning
    > depending on includes and using statements.


    What do you mean by "an ordinary identifier in C++"? It (String with a
    capital S) is not even a standard class.

    [snip]

    > The easiest way may be to use sscanf() in <cstdio>. A purist would probably
    > use something in <iostream>, it's been a while since I did this, from
    > memory, perhaps istrstream


    It's ostringstream. (the istrstream and ostrstream are superseded already.)

    Ben
     
    benben, Sep 10, 2006
    #5
  6. compboy

    osmium Guest

    "benben" writes:

    > [snip]
    >>>
    >>> String aa = 34;

    >>
    >> String is just an ordinary identifier in C++. string has a special
    >> meaning depending on includes and using statements.

    >
    > What do you mean by "an ordinary identifier in C++"? It (String with a
    > capital S) is not even a standard class.


    That's why it's an ordinary identifer.

    Similar to x, bftsklk, temp, ....
     
    osmium, Sep 10, 2006
    #6
  7. compboy

    Jim Langston Guest

    "compboy" <> wrote in message
    news:...
    > Can anyone help me about this.
    >
    > I have been trying all the ways I knew and I could find but just didnt
    > work.
    >
    > I have tried:
    >
    > using itoa but it says that it doesnt have that function.
    >
    > and also
    >
    > String aa = 34;
    >
    > cout << aa.val() <endl;
    >
    > but again just didnt work out it even cant find the String as a
    > declaration.
    > I tried to change it into string but still didnt work
    >
    > and I tried to do it like c:
    >
    > aa = 30+" ";
    > and still didnt work.
    >
    > just for information that I have included all the libraries I need for
    > those functions mentioned above.
    >
    > Thanks a lot.


    The way I do it is using stringstreams.
    You need to
    #include <sstream>

    std::string aa;
    int Value = 34;

    Now say I want to get value, which is an int, into the string as the
    characters "34"
    std::stringstream ConvertStream;
    ConvertStream << Value;
    ConvertStream >> aa;

    At this point the std::string aa contains the text "34" if all went well.

    I find this so useful, in fact, that I have found a template that I use.
    This is my template:

    template<typename T, typename F > T StrmConvert( F from )
    {
    std::stringstream temp;
    temp << from;
    T to = T();
    temp >> to;
    return to;
    }

    template<typename F> std::string StrmConvert( F from )
    {
    return StrmConvert<std::string>( from );
    }

    First, to talk about the first template, which takes two typenames. One is
    what to convert To, the other is what to convert from. I could do the same
    thing as before doing this:
    std::string aa;
    int Value = 34;
    aa = StrmConvert<std::string>( Value );

    I find, however, that most of the time I am converting some type of number
    to std::string and I got tired of always specifying std::string, so that's
    where the specialized template comes from. If I don't specify what to
    convert to, it converts it to std::string, so then it becomes even shorter:

    std::string aa;
    int Value = 34;
    aa = StrmConvert( Value );

    But I could still go the other way if I wanted, std::string to number:
    Value = StrmConvert<int>( aa );

    This works for all built in types such as int, float, double, char, etc...

    The observant may notice that I am not doing any error checking in the
    template. That is, if the string was "xyz" and I tried to convert it to a
    number the stringstream would be in an error condition. Because of the
    line:
    T to = T();
    the to variable is being default initialized. If it's int, double, char, it
    will be set to 0. std::string "", etc...
     
    Jim Langston, Sep 11, 2006
    #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. Jon Paskett

    Convert a Base32 string to Base10 integer

    Jon Paskett, Dec 18, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    1,168
    Nathan Sokalski
    Dec 18, 2005
  2. Azmie
    Replies:
    12
    Views:
    1,068,784
    Azmie
    Dec 30, 2003
  3. les ander
    Replies:
    4
    Views:
    921
    wes weston
    Oct 5, 2004
  4. news
    Replies:
    2
    Views:
    1,134
  5. Randy Kramer
    Replies:
    12
    Views:
    403
    Robert Klemme
    Oct 25, 2007
Loading...

Share This Page