Text from Textbox covert from String^ to int

2019-09-05 12:33发布

问题:

Currently I'm making an application and try to get a value from a textBox and convert it then to an integer for further use. I have the following code:

System::String^ maxTTL = textBoxMaxTTL->Text; 
std::string bla = marshal_as<std::string>(maxTTL); //System string^ to std::string
int maxTTL2 = std::atoi(bla.c_str());

It seems that maxTTL2 still got the value of '0'. When I use the stoi argument it throws the following exception: stoi argument out of range. Has somebody an idea to resolve this?

回答1:

I think you can just do:

int i = Int32::Parse(maxTTL);

Afterwards you will get the int in i. You can also bulletproof the code with a try block.



回答2:

I think you need std::stoi() as you have a std::string type to convert to int.

Here is the documentation you need for std::stoi().

So your line should look like: int maxTTL2 = std::stoi(bla);