D
David Ching
Hello,
I'm a .NET newbie but have 12 years of C++/MFC.
In C++, atoi() stops converting at the first non-digit, so in this code,
octet will be 192:
int octet = atoi("192."); // <-- note '.' at end of this string
This is great, exactly what I want. But to achieve it in C++/CLI, the code
I am using is this:
try
{
int octet = Convert::ToInt16("192.");
}
catch (Exception^ )
{
// Convert::ToInt16 throws when there are non-digits in parameter
}
Because an exception is thrown, the conversion is not successful. This is
highly unusable! I've had to strip off the '.' so Convert::ToInt16() is
successful in the normal case.
However, if the string is malformed (doesn't contain any digits), the
exception is still thrown, whereas atoi() simply returns a value which I
skip. Maybe I'm misunderstanding exceptions (I don't use them even in C++
when I can help it), but having to try/catch an exception instead of just
getting a simple return value seems to be adding complexity instead of
reducing it. I don't like replacing 1 line of C++ with 8 lines of .NET for
a simple conversion.
Is there a better way?
Thanks,
David (MVP - VC++)
I'm a .NET newbie but have 12 years of C++/MFC.
In C++, atoi() stops converting at the first non-digit, so in this code,
octet will be 192:
int octet = atoi("192."); // <-- note '.' at end of this string
This is great, exactly what I want. But to achieve it in C++/CLI, the code
I am using is this:
try
{
int octet = Convert::ToInt16("192.");
}
catch (Exception^ )
{
// Convert::ToInt16 throws when there are non-digits in parameter
}
Because an exception is thrown, the conversion is not successful. This is
highly unusable! I've had to strip off the '.' so Convert::ToInt16() is
successful in the normal case.
However, if the string is malformed (doesn't contain any digits), the
exception is still thrown, whereas atoi() simply returns a value which I
skip. Maybe I'm misunderstanding exceptions (I don't use them even in C++
when I can help it), but having to try/catch an exception instead of just
getting a simple return value seems to be adding complexity instead of
reducing it. I don't like replacing 1 line of C++ with 8 lines of .NET for
a simple conversion.
Is there a better way?
Thanks,
David (MVP - VC++)