opposite to atoi.

This is a discussion on opposite to atoi. within the C++ Programming forums, part of the General Programming Boards category; I know that the atoi() function converts a string number to an int equivalent. Is there a similar function that ...

  1. #1
    Registered User Mark S.'s Avatar
    Join Date
    May 2005
    Location
    England
    Posts
    16

    opposite to atoi.

    I know that the atoi() function converts a string number to an int equivalent.

    Is there a similar function that converts an int number to a string number.

    Thanks

    Mark S.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Quote Originally Posted by Mark S.
    I know that the atoi() function converts a string number to an int equivalent.

    Is there a similar function that converts an int number to a string number.

    Thanks

    Mark S.
    Yep, they're called stringstreams, and they work both ways. There is also a non-standard function known as itoa(), but forget about that. Also, forget about atoi() while you're at it unless you're interested in C. The C++ way is stringstreams.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    boost::lexical_cast is another choice.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The C pendant to atoi is sprintf. But for C++, stick with the streams or lexical_cast.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I know that the atoi() function converts a string number to an int equivalent.
    That's the theory behind atoi. If you're wise you'll forget it exists. In C it has awful error handling semantics, and strtol (or even sscanf) is the recommended alternative. In C++ there are much better and safer ways to do this (such as stringstreams or the lovely boost::lexical_cast) that are consistent in both directions, unlike the C solution.
    My best code is written with the delete key.

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Go for string stream! I think there's an itoa() function, unless I'm making that up, but I don't think I am. stringstreams are great!

    Here's a nice example that Prelude used the other day to put a number into a string :: improve my number splitting prog You can Google stringstreams, or there should be a tutorial here somewhere. This is probably helpful http://www.cplusplus.com/ref/iostream/stringstream/

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Here's a nice example that Prelude used the other day to put a number into a string
    It's only slightly more complicated and vastly more useful to generalize:
    Code:
    #include <sstream>
    #include <typeinfo>
    
    namespace jsw {
      template <typename T, typename U>
      T lexical_cast ( U src )
      {
        std::stringstream s;
        T dst;
    
        if ( !( s << src ) || !( s >> dst ) )
          throw std::bad_cast ( "Invalid conversion" );
    
        return dst;
      }
    }
    Of course, that's nothing more than a simplified version of what boost::lexical_cast does.
    My best code is written with the delete key.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I think there's an itoa() function, unless I'm making that up, but I don't think I am.
    If you compile as ANSI-compatible, then your compiler will complain that you are. It's a non-standard function, although cppreference.com has it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

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. opposite to atoi() ?
    By Milhas in forum C Programming
    Replies: 5
    Last Post: 03-30-2008, 04:20 PM
  3. help with this
    By tyrantil in forum C Programming
    Replies: 18
    Last Post: 01-30-2005, 03:53 PM
  4. Problem with atoi in C
    By garagebrian in forum C Programming
    Replies: 5
    Last Post: 08-24-2004, 01:59 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