atoi() equivilent without exceptions?

  • Thread starter Thread starter David Ching
  • Start date Start date
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++)
 
David Ching said:
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++)



Use the TryParse method like this:

int result;
if(Int32::TryParse("129.", result) == true)
Console::WriteLine("{0}", result);
else
...
or the overload which allows you specify a Number style or/and a FormatProvider.

Willy.
 
Willy Denoyette said:
Use the TryParse method like this:

int result;
if(Int32::TryParse("129.", result) == true)
Console::WriteLine("{0}", result);
else
...

Thanks much Willy it worked somewhat. TryParse() returned false for "129."
since it didn't like the '.' at the end. Only whitespace is allowed after
the digits. But if I kept my code to remove the '.', then it worked, and I
don't have to try/catch an exception, saving code.

I need to look for the methods starting with "Try", as these seem to be the
ones that don't throw exceptions.

Thanks again,
David (MVP - VC++)
 
Hi David!
Thanks much Willy it worked somewhat. TryParse() returned false for "129."
since it didn't like the '.' at the end. Only whitespace is allowed after
the digits. But if I kept my code to remove the '.', then it worked, and I
don't have to try/catch an exception, saving code.

Take a look at the "NumberStyles" enumeration!

The following works without any modification:

int i = 0;
Console::WriteLine(
Int32::TryParse("129.",
Globalization::NumberStyles::Number,
nullptr,
i)
);
Console::WriteLine(i);

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
Jochen Kalmbach said:
Take a look at the "NumberStyles" enumeration!

The following works without any modification:

int i = 0;
Console::WriteLine(
Int32::TryParse("129.",
Globalization::NumberStyles::Number,
nullptr,
i)
);
Console::WriteLine(i);

Thanks for the tip Jochen! Much appreciated.

-- David
 
Back
Top