linda23
asked on
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.
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.
You can use atoi.
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
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
>>>> You can use atoi.
With managed code?
With managed code?
>>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.
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.
>>>> I'm not that familiar with managed code
So we are at least two ;-)
But I am trying ...
So we are at least two ;-)
But I am trying ...
ASKER
itsmeandnobodyelse: Your code doesn't compile..."get_Chars" doesn't exist.
ASKER
How could I use "atoi"?
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
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...
ASKER
Axter: That's the stuff I was looking for. Great!
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';
}
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';
}
>>>> but C# is heaven compared to this...
Forgive me. I am a learner ...
Forgive me. I am a learner ...
>>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.
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.
>>>> 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
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
int x = 0;
for (int i = 0; i < s.Length(); ++i)
{
x *= 10;
x += s[i] - '0';
}
Regards, Alex