Thread: atoi() equivalent in C++

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    10

    atoi() equivalent in C++

    I know that :

    Code:
    int  atoi ( const char * string );
    atoi() converts a string to an integer in C programming.

    Is there an equivalent in C++ programming?

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    In C++, it is not that easy anymore.
    You can make your own atoi, but you have to use stringstreams (it's the standard):
    Code:
    #include <sstream>
    
    int AtoI(char *input){
    	int temp;
    	stringstream ssout(*input);
    	ssout>>temp;
    	return temp;
    }
    Warning!! I have not tested it.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    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

  4. #4
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    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

  5. #5
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    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

  6. #6
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Argue with someone who has more experience...
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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.

  9. #9
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    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

  10. #10
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by Prelude
    >Why not using atoi() in C++?
    Because it sucks just as badly in C++ as it does in C.
    itoa()
    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

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    itoa()
    If your buffer's size is at least 12 bytes, there can be no buffer overflow.
    itoa() is non-standard in both C and C++.

    atoi()
    How can it have any buffer overflow, if it is not doing anything to buffers?
    Where did Prelude mention buffer overflow in this thread?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by laserlight
    Where did Prelude mention buffer overflow in this thread?
    Salem did.

    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

  13. #13
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    > 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.

  14. #14
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Salem didn't mention buffer overflow, he said overflow checking.
    Overflow checking checks for buffer overflow (that means, avoids it), doesn't it?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Overflow checking checks for buffer overflow (that means, avoids it), doesn't it?
    No, in this case 'overflow' is with respect to arithmetic, not character buffers.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble with atoi
    By NewbGuy in forum C Programming
    Replies: 2
    Last Post: 05-22-2009, 11:55 PM
  2. Pointer equivalent to array notation
    By bekkilyn in forum C Programming
    Replies: 4
    Last Post: 12-06-2006, 08:22 PM
  3. Replies: 10
    Last Post: 08-17-2005, 11:17 PM
  4. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  5. string to int conversion not using atoi()
    By linucksrox in forum C Programming
    Replies: 2
    Last Post: 05-19-2004, 12:17 AM