[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 263
  • Last Modified:

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


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 :)
1 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();

shinymoonAuthor Commented:
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!

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now