Text from Textbox covert from String^ to int

二次信任 提交于 2019-12-11 19:24:38

问题


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);



来源:https://stackoverflow.com/questions/17341479/text-from-textbox-covert-from-string-to-int

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!