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?
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; }
"The Internet treats censorship as damage and routes around it." - John Gilmore
Why not using atoi() in C++?
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
I don't know exactly. It's not a good habit to use C standards in a C++ program...
"The Internet treats censorship as damage and routes around it." - John Gilmore
C++ is based on C. atoi() is also standard in C++.
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
Argue with someone who has more experience...
"The Internet treats censorship as damage and routes around it." - John Gilmore
>Why not using atoi() in C++?
Because it sucks just as badly in C++ as it does in C.
My best code is written with the delete key.
> 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
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
atoi_s()?
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
itoa()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...
"The Internet treats censorship as damage and routes around it." - John Gilmore
itoa() is non-standard in both C and C++.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?atoi()
How can it have any buffer overflow, if it is not doing anything to buffers?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Salem did.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.
"The Internet treats censorship as damage and routes around it." - John Gilmore
> 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
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
Overflow checking checks for buffer overflow (that means, avoids it), doesn't it?Salem didn't mention buffer overflow, he said overflow checking.
"The Internet treats censorship as damage and routes around it." - John Gilmore
No, in this case 'overflow' is with respect to arithmetic, not character buffers.Overflow checking checks for buffer overflow (that means, avoids it), doesn't it?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)