2

Hey! I have textBox with text like "12:30" and this code textBox -> Text -> ToString() -> Split(':')[1] It return "30" as string. And I want convert it to Int. How? I founded function like Convert::ToInt32() etc, but it doesnt work for my c++ (Visual C++ 2010 -> Winfow Form). Help me plz! (I started learn c++ 2 days ago)

And i use Managed C++

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
MegaFill
  • 25
  • 1
  • 1
  • 5

3 Answers3

3

As you're using Managed C++, then you can do this:

double foo = System::Convert::ToDouble("200");
int bar = System::Convert::ToInt32("200");

Use whatever you need!

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • If I include `#include` after my `using namespace System;`, I get a lot of errors... – MegaFill Jan 15 '11 at 11:40
  • @MegaFill : you should have tell that you're using Managed C++. Not Visual C++. – Nawaz Jan 15 '11 at 11:48
  • @MegaFill: You *could* do this. If you want to throw an exception if the number you're trying to convert to a string is not actually a number. What if the user typed `AA:BB` into the textbox? **You're actually looking for [`Int32::TryParse`](http://msdn.microsoft.com/en-us/library/f02979c7.aspx).** – Cody Gray - on strike Jan 15 '11 at 12:29
1

you can use c standard lib frunction atoi

CString s = "30";
int x = atoi( s ); // x is now 30

Edit: Oh, your are using managed C++, then one of the following two should do the job

System::Convert::ToInt32(str, 10);
System::Int32::Parse(str);

Refer to this page with an example: http://msdn.microsoft.com/en-us/library/b3h1hf19.aspx

ak.
  • 3,329
  • 3
  • 38
  • 50
1

I use

int intVar = Int32::Parse(stringVar);
DPD
  • 1,734
  • 2
  • 19
  • 26