473,385 Members | 1,449 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

atoi() equivilent without exceptions?

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++)
Mar 3 '07 #1
5 5110
>Is there a better way?

Since it's C++/CLI, keep using atoi.

Dave
Mar 3 '07 #2
"David Ching" <dc@remove-this.dcsoft.comwrote in message
news:dw***************@newssvr14.news.prodigy.net. ..
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.

Mar 3 '07 #3
"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
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++)
Mar 4 '07 #4
Hi David!
>>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.
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/
Mar 4 '07 #5
"Jochen Kalmbach [MVP]" <no********************@holzma.dewrote in message
news:uD**************@TK2MSFTNGP06.phx.gbl...
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
Mar 4 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Mike Moum | last post by:
I think there may be a bug in string.atoi and string.atol. Here's some output from idle. > Python 2.3.4 (#2, Jan 5 2005, 08:24:51) > on linux2 > Type "copyright", "credits" or "license()"...
8
by: Sonia | last post by:
Hi, I've been using atoi for a while now, but would like to know how to implement one? Can anyone give me simple efficient implementation of that function? Or point me sowhere for reference. ...
4
by: Darren | last post by:
Hi. I have a javascript menu system which uses the "elementFromPoint" function. However Netscape/Mozilla doesn't recognise this function. Does it have an equivilent? Thanks -- Darren
2
by: francescomoi | last post by:
Hi. I'm trying to compile this piece of source: ------------------------------------------- int id; while(row1 = mysql_fetch_row(rs1)) { id = atoi((int)row1);...
11
by: rayw | last post by:
I'm pretty new to C, although I did do some years ago now. I've been told that itoa is no longer a standard function, and that the ato... functions - although in the std - are not recommended. ...
23
by: tolkien | last post by:
Hi,My problem is this: I have : char matrix=.... int x; x=atoi(matrix); This doesn't work.Any help? I don't have any errors.Just a warning" passing argument 1 of 'atoi' makes pointer from...
11
by: Nezhate | last post by:
Hi all, Can you help me? Why this warning appears in the next simple code ? warning: passing argument 1 of ‘atoi’ makes pointer from integer without a cast. #include <stdio.h> #include...
4
by: Ram | last post by:
Hi All, Firstly i am a newbie and trying to learn C. The background of the problem is Program: Presently I am working on a program of numerology and the I/P will be the name and output...
50
by: Bill Cunningham | last post by:
I have just read atoi() returns no errors. It returns an int though and the value of the int is supposed to be the value of the conversion. It seems to me that right there tells you if there was...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.