• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 495
  • Last Modified:

String to int in c++ net

How can I convert a System::String to an int in c++ .Net?

I have a string called param that contains a number (e.g. "32") and I want to get a int called x. Please show explicit code.
0
linda23
Asked:
linda23
  • 7
  • 4
  • 4
1 Solution
 
itsmeandnobodyelseCommented:
   String s = "123";
    int x = 0;
    for (int i = 0; i < s.Length(); ++i)
    {
         x *= 10;
         x += s[i] - '0';
    }

Regards, Alex
0
 
AxterCommented:
You can use atoi.

0
 
itsmeandnobodyelseCommented:
If it doesn't compile you may try that:

    String s = L"123";
    int x = 0;
    for (int i = 0; i < s.Length(); ++i)
    {
         x *= 10;
         x += s.get_Chars(i) - L'0';
    }

if you want to check for valid chars you might add a check like that:

        wchar_t wc = s.get_Chars(i);
        if (wc < L'0' || wc > L'9')
             break;  // or error handling
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
itsmeandnobodyelseCommented:
>>>> You can use atoi.
With managed code?
0
 
AxterCommented:
>>With managed code?

I'm not that familiar with managed code, but I would be very surprise if it doesn't support atoi, or a comparable functions, since most languages support this in one form or another.
0
 
itsmeandnobodyelseCommented:
>>>> I'm not that familiar with managed code
So we are at least two ;-)

But I am trying ...
0
 
linda23Author Commented:
itsmeandnobodyelse: Your code doesn't compile..."get_Chars" doesn't exist.
0
 
linda23Author Commented:
How could I use "atoi"?
0
 
AxterCommented:
A quick google search shows a couple of methods specific to managed code.

Method #1:  (Use Convert::ToInt32)
int a = 0;

Console::WriteLine(L"Enter an integer");

String^ b = Console::ReadLine();

a = Convert::ToInt32(b);


Method #2: (Use System::Int32::Parse)
int a = 0;

Console::WriteLine(L"Enter an integer");

String^ b = Console::ReadLine();

a = Use System::Int32::Parse(b);

0
 
linda23Author Commented:
I can't understand why this has to be so complicated... I hope I don't offend anyone, but C# is heaven compared to this...
0
 
linda23Author Commented:
Axter: That's the stuff I was looking for. Great!
0
 
itsmeandnobodyelseCommented:
I found some sample code as well:

   String* str = S"123";
   Char arr[];

    arr = str->ToCharArray(0, str->Length());
    int x = 0;
    for (int i = 0; i < str->Length(); ++i)
    {
         x *= 10;
         x += arr[i] - L'0';
    }
0
 
itsmeandnobodyelseCommented:
>>>> but C# is heaven compared to this...
Forgive me. I am a learner ...
0
 
AxterCommented:
>>I hope I don't offend anyone, but C# is heaven compared to this...

You can blame this on MS.  C++ is the least supported language for .Net.
Just look at the code examples in MSDN for C++.
Sometimes they don't even have examples for C++, but they always seem to have examples for the other supported languages.
When they do have examples, either the example don't apply to what you're looking for, or they don't give you enough information to get it working in your code.

IMHO, C++ was an after thought, or a low priority supported language for MS, and I wouldn't be surprise if this was done intentionally so that they could promote C# over C++.
After all, what incentive would you have to move to C#, if you got the same functionality in C++ with manage code.
0
 
itsmeandnobodyelseCommented:
>>>> You can blame this on MS.  
If we assume that the main reason for .NET was to 'upgrade' the millions of VB developers to 'real' programmers and that C# was the language to attract java programmers, there are not so much programmers left. I doubt that C++ programmers can become friends with managed code, especially if the realize that they can throw away any experience, start as a beginner, and finally realize that some things they want to do cannot be made with managed code, let aside that any portability considerations were needless anyway. I earn my money with Microsoft compilers but .NET makes me crazy.

Regards, Alex
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 7
  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now