[vc++ .net 2005] converting from int to std::string

Posted on 2007-08-02
Last Modified: 2013-11-26

In managed c++, how can I convert int to std::string?? I have a short int type variable and I need to do something like "str += shortIntValue"

Thank you :)
Question by:shinymoon
    LVL 19

    Accepted Solution

    If you are doing managerd C++, do you really want a std::string or do you want a System::String?

    If using a System::String, you can do:

        str += shortIntValue; // Runtime calls shortIntValue.ToString() for you.

    If using a std::string, you need to use _itoa to create a string first and then

        char shortIntValueAsString[16];
        ::_itoa(shortIntValue, shortIntValueAsString, 10);
      // or use new MS "safe" version: ::_itoa_s(shortIntValue, shortIntValueAsString, 16, 10);  
        str += shortIntValueAsString;

    Or you can use a ostringstream:

        std::ostringstream strstr;
        strstr << shortIntValue;
        str += strstr.str();


    Author Comment

    Yes! It worked!!
    I was using std::string. I used the second solution that is to use ostringstream. Only thing was I had to add header #include <sstream>. That's it.
    Thank you so much, drichards!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    This video discusses moving either the default database or any database to a new volume.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now