integer to string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AC 74

    integer to string

    I read from a command line using g++. I changed the argv to integers
    using atoi() in order to add the numbers. Now I need to change them
    from int to string and itoa is not supported. Can somebody please
    help me to change these integers back to strings? Thanks.
  • Jonathan Turkanis

    #2
    Re: integer to string

    "AC 74" <AC74ECU@hotmai l.com> wrote in message
    news:dabf606f.0 402102107.95334 0a@posting.goog le.com...[color=blue]
    > I read from a command line using g++. I changed the argv to[/color]
    integers[color=blue]
    > using atoi() in order to add the numbers. Now I need to change them
    > from int to string and itoa is not supported. Can somebody please
    > help me to change these integers back to strings? Thanks.[/color]

    This is a famous FAQ question:
    http://www.parashift.com/c++-faq-lit....html#faq-38.1.

    Jonathan


    Comment

    • Sumit Rajan

      #3
      Re: integer to string


      "AC 74" <AC74ECU@hotmai l.com> wrote in message
      news:dabf606f.0 402102107.95334 0a@posting.goog le.com...[color=blue]
      > I read from a command line using g++. I changed the argv to integers
      > using atoi() in order to add the numbers. Now I need to change them
      > from int to string and itoa is not supported. Can somebody please
      > help me to change these integers back to strings? Thanks.[/color]


      Try the FAQ:



      Regards,
      Sumit.


      Comment

      • Mike Wahler

        #4
        Re: integer to string


        "AC 74" <AC74ECU@hotmai l.com> wrote in message
        news:dabf606f.0 402102107.95334 0a@posting.goog le.com...[color=blue]
        > I read from a command line using g++. I changed the argv to integers
        > using atoi() in order to add the numbers. Now I need to change them
        > from int to string and itoa is not supported. Can somebody please
        > help me to change these integers back to strings? Thanks.[/color]

        #include <iostream>
        #include <sstream>
        #include <string>

        int main()
        {
        int i(42);
        std::ostringstr eam oss;
        oss << i;
        std::string s(oss.str());
        std::cout << s << '\n'; // prints 42
        return 0;
        }

        -Mike



        Comment

        • james

          #5
          Re: integer to string

          _itoa() is a easiest function,format is as below: you have to include
          <stdlib.h>
          char *_itoa( int value, char *string, int radix );

          char *_i64toa( __int64 value, char *string, int radix );

          char * _ui64toa( unsigned _int64 value, char *string, int radix );

          wchar_t * _itow( int value, wchar_t *string, int radix );

          wchar_t * _i64tow( __int64 value, wchar_t *string, int radix );

          wchar_t * _ui64tow( unsigned __int64 value, wchar_t *string, int radix );

          "AC 74" <AC74ECU@hotmai l.com> ???
          news:dabf606f.0 402102107.95334 0a@posting.goog le.com ???...[color=blue]
          > I read from a command line using g++. I changed the argv to integers
          > using atoi() in order to add the numbers. Now I need to change them
          > from int to string and itoa is not supported. Can somebody please
          > help me to change these integers back to strings? Thanks.[/color]


          Comment

          • Peter van Merkerk

            #6
            Re: integer to string

            "james" <jj6066@dcs.war wick.ac.uk> wrote in message
            news:c0dhq8$1dg $1@mail.dcs.war wick.ac.uk...[color=blue]
            > _itoa() is a easiest function,format is as below: you have to include
            > <stdlib.h>
            > char *_itoa( int value, char *string, int radix );
            >
            > char *_i64toa( __int64 value, char *string, int radix );
            >
            > char * _ui64toa( unsigned _int64 value, char *string, int radix );
            >
            > wchar_t * _itow( int value, wchar_t *string, int radix );
            >
            > wchar_t * _i64tow( __int64 value, wchar_t *string, int radix );
            >
            > wchar_t * _ui64tow( unsigned __int64 value, wchar_t *string, int radix );[/color]

            This is not a standard function (the leading underscore is a hint that it
            is non-standard). Even though the library that came with your compiler has
            this function, it is not defined in the C++ standard. Other implementations
            of the standard library may or may not support this function.

            --
            Peter van Merkerk
            peter.van.merke rk(at)dse.nl



            Comment

            • Evan Carew

              #7
              Re: integer to string

              -----BEGIN PGP SIGNED MESSAGE-----
              Hash: SHA1

              AC,

              try the Boost lib's safe cast operator:



              AC 74 wrote:[color=blue]
              > I read from a command line using g++. I changed the argv to integers
              > using atoi() in order to add the numbers. Now I need to change them
              > from int to string and itoa is not supported. Can somebody please
              > help me to change these integers back to strings? Thanks.[/color]

              -----BEGIN PGP SIGNATURE-----
              Version: GnuPG v1.0.6 (GNU/Linux)
              Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

              iD8DBQFAKpueoo/Prlj9GScRAmd0AJ 9bvStPa7OsTPzsP k9tt9li8nylgACf VEWD
              q0Mu/Ov7vN2s5u5PVj6w pY8=
              =9pom
              -----END PGP SIGNATURE-----

              Comment

              Working...