I know that :
atoi() converts a string to an integer in C programming.Code:int atoi ( const char * string );
Is there an equivalent in C++ programming?
Printable View
I know that :
atoi() converts a string to an integer in C programming.Code:int atoi ( const char * string );
Is there an equivalent in C++ programming?
In C++, it is not that easy anymore.
You can make your own atoi, but you have to use stringstreams (it's the standard):
Warning!! I have not tested it.Code:#include <sstream>
int AtoI(char *input){
int temp;
stringstream ssout(*input);
ssout>>temp;
return temp;
}
Why not using atoi() in C++?
I don't know exactly. It's not a good habit to use C standards in a C++ program...
C++ is based on C. atoi() is also standard in C++.
Argue with someone who has more experience...
>Why not using atoi() in C++?
Because it sucks just as badly in C++ as it does in C. :)
> Why not using atoi() in C++?
Because it has no error checking, or overflow checking.
strtol() would be OK I think.
Use stringstream or a lexical cast
http://www.boost.org/libs/conversion/lexical_cast.htm
atoi_s()?
itoa()Quote:
Originally Posted by Prelude
If your buffer's size is at least 12 bytes, there can be no buffer overflow.
atoi()
How can it have any buffer overflow, if it is not doing anything to buffers?
You can only make atoi() or itoa() "fail", if you make a serious bug in your program...
itoa() is non-standard in both C and C++.Quote:
itoa()
If your buffer's size is at least 12 bytes, there can be no buffer overflow.
Where did Prelude mention buffer overflow in this thread?Quote:
atoi()
How can it have any buffer overflow, if it is not doing anything to buffers?
Salem did.Quote:
Originally Posted by laserlight
I am not saying it is a good idea to use itoa() and atoi(), but that you shouldn't have other problems with it, except that you're writing unstandard code.
> itoa() If your buffer's size is at least 12 bytes, there can be no buffer overflow.
itoa is not standard.
> atoi() How can it have any buffer overflow, if it is not doing anything to buffers?
Salem didn't mention buffer overflow, he said overflow checking.
Code:char strnumber[50];
/*... fill the number ... */
int number = atoi(strnumber); // results in undefined behavior
Overflow checking checks for buffer overflow (that means, avoids it), doesn't it?Quote:
Salem didn't mention buffer overflow, he said overflow checking.
No, in this case 'overflow' is with respect to arithmetic, not character buffers.Quote:
Overflow checking checks for buffer overflow (that means, avoids it), doesn't it?