Converting String^ to Int

Standard ‘learning the language’ warning: This isn’t C++ you’re writing, it’s C++/CLI. C++/CLI is a language from Microsoft intended to allow C# or other .Net languages to interface with unmanaged C++. In that scenario, C++/CLI can provide the translation between the two. If you’re still learning C++, please do not start with C++/CLI. In order to effectively write in C++/CLI, one should already know both C++ and C#, and then there’s still things to learn about C++/CLI. If you want to learn C++, stick with unmanaged C++. (In Visual Studio, create a “Win32” C++ project.) If you want to learn managed code, then I would use C#.


That said: Int32::TryParse is how you convert from a .Net String^ to an integer.

String^ tempString = ...;
int value;

bool converted = Int32:::TryParse(tempString, value);
if (!converted)
{
    // tempString did not contain a valid integer. Handle the error.
}

Leave a Comment