atoi() equivalent in C++

This is a discussion on atoi() equivalent in C++ within the C++ Programming forums, part of the General Programming Boards category; I know that : Code: int atoi ( const char * string ); atoi() converts a string to an integer ...

  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,231
    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,231
    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,796
    >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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    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
    21,310
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Portugal
    Posts
    7,403
    > 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
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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
    21,310
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
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, 07: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, 10: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21