LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   cast from sting to int in Visual C++ (https://www.linuxquestions.org/questions/programming-9/cast-from-sting-to-int-in-visual-c-387546/)

Diederick 11-29-2005 06:29 AM

cast from sting to int in Visual C++
 
Hi

I am wrking in Visual C++ (Visual Studio.NET 2003), and i am struggeling to case a String that I read from a text box to an integer (int). Can somone please help me with this.

Thanx D:(

Wim Sturkenboom 11-29-2005 07:15 AM

Afaik the string has a method strtol (or something similar). Else there's the C-function strtol or strtoul.

graemef 11-29-2005 07:54 AM

.Net has the following method
Code:

public: static short ToInt16(String* value);
depending on the type of integer you want there is also ToInt32 and ToInt64.

These are all documented on the MSDN siteConvert Class .

However if you want your code to be portable to another framework then you may want to look at the strtol() function, as previously mentioned, as this is part of the standard C library so #include <cstdlib>.

graeme.

graemef 11-29-2005 08:27 AM

A gentle request to please keep your questions in the forum
 
Quote:

When I type:

int var1 = txtTextBox->get_Text()->toInt32(###);

the ### wants an System::IFormatProvider. What is that and how can i use it to convert my string to an integer.
You probably want to change your code to:

Code:

int var1 = Convert::toInt32(txtTextBox->get_Text());
However a IFormatProvider is a class that will tell how you want the number formatted. That is will it include the sign, how about thousand separators, do you want a monetary symbol, etc.

The IFormatProvider is an optional parameter (if you use the static version of toInt32).

I hope that helps, an example of the code to set up an IFormatProvider is given at page to the C++ example

graeme


All times are GMT -5. The time now is 10:58 PM.