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

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

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

Hello!

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 :)
0
shinymoon
Asked:
shinymoon
1 Solution
 
drichardsCommented:
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();

0
 
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!
0

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