How to convert from number to text in C++?

Discussion in 'C++' started by Huyvtq, Jan 6, 2007.

  1. Huyvtq

    Huyvtq Guest

    What's the function I can use ?? Help me!
    Thanks.
    Huyvtq, Jan 6, 2007
    #1
    1. Advertising

  2. Huyvtq

    Jim Langston Guest

    "Huyvtq" <> wrote in message
    news:...
    > What's the function I can use ?? Help me!
    > Thanks.


    The best choice, IMO, is to use a stringstream.

    std::stringstream Convert;
    Convert << 12345;
    std::string NumAsString;
    Convert >> NumAsString;

    At this point the std::string Convert will contain "12345".

    Another, although I feel poorer choice, is itoa which uses char arrays
    rather than std::string.
    Jim Langston, Jan 6, 2007
    #2
    1. Advertising

  3. Huyvtq

    JeffCameron Guest

    #include <stdio.h>
    #include <string>
    ....

    int number;
    char s1[256];

    number = 42;
    sprintf(s1, "%d", number);

    std::string s = new std::string(s1);
    JeffCameron, Jan 6, 2007
    #3
  4. Jim Langston wrote:
    > "Huyvtq" <> wrote in message
    > news:...
    > > What's the function I can use ?? Help me!
    > > Thanks.

    >
    > The best choice, IMO, is to use a stringstream.
    >
    > std::stringstream Convert;
    > Convert << 12345;
    > std::string NumAsString;
    > Convert >> NumAsString;
    > ...


    I agree with the use of a stringstream, but you can most likely skip
    the Convert >> NumAsString part and use Convert.str(); to obtain a a
    string from the stringstream.
    Shawn McGrath, Jan 6, 2007
    #4
  5. Huyvtq

    Jim Langston Guest

    "JeffCameron" <> wrote in message
    news:...
    > #include <stdio.h>


    #include <cstdio>

    > #include <string>
    > ...
    >
    > int number;
    > char s1[256];
    >
    > number = 42;
    > sprintf(s1, "%d", number);


    std::sprintf( s1, "%d", number );

    > std::string s = new std::string(s1);


    And, yes, that is another way to convert to a char array.

    But, std::strings are prefered.
    Jim Langston, Jan 6, 2007
    #5
  6. JeffCameron schrieb:
    > #include <stdio.h>
    > #include <string>
    > ...
    >
    > int number;
    > char s1[256];
    >
    > number = 42;
    > sprintf(s1, "%d", number);
    >
    > std::string s = new std::string(s1);


    Will not compile.
    "new std::string" returns a pointer.

    --
    Thomas
    http://www.netmeister.org/news/learn2quote.html
    Thomas J. Gritzan, Jan 6, 2007
    #6
  7. Huyvtq

    Jim Langston Guest

    "Thomas J. Gritzan" <> wrote in message
    news:ennorf$ghg$...
    > JeffCameron schrieb:
    >> #include <stdio.h>
    >> #include <string>
    >> ...
    >>
    >> int number;
    >> char s1[256];
    >>
    >> number = 42;
    >> sprintf(s1, "%d", number);
    >>
    >> std::string s = new std::string(s1);

    >
    > Will not compile.
    > "new std::string" returns a pointer.


    Oooh, I missed that one myself.
    Jim Langston, Jan 6, 2007
    #7
  8. Huyvtq

    Rolf Magnus Guest

    Jim Langston wrote:

    > "Huyvtq" <> wrote in message
    > news:...
    >> What's the function I can use ?? Help me!
    >> Thanks.

    >
    > The best choice, IMO, is to use a stringstream.
    >
    > std::stringstream Convert;
    > Convert << 12345;
    > std::string NumAsString;
    > Convert >> NumAsString;
    >
    > At this point the std::string Convert will contain "12345".
    >
    > Another, although I feel poorer choice, is itoa which uses char arrays
    > rather than std::string.


    itoa is also not a standard function, so it's non-portable.
    Rolf Magnus, Jan 6, 2007
    #8
  9. Huyvtq

    JeffCameron Guest

    Ah yes a slip of the tongue. Leave the new keywork out.

    Jeff Cameron
    JeffCameron, Jan 7, 2007
    #9
  10. Huyvtq

    Grizlyk Guest

    Grizlyk, Jan 12, 2007
    #10
  11. Huyvtq

    Greg Guest

    Grizlyk wrote:
    > JeffCameron ÐÉÓÁÌ(Á):
    >
    > > int number;
    > > char s1[256];
    > >
    > > number = 42;
    > > sprintf(s1, "%d", number);

    >
    > snprintf can be used (if implemented) as safe version of sprintf


    In a conforming C++ implementation, std::snprintf() will always be
    implemented. The routine is declared in <cstdio>.

    std::snprintf() is a much safer choice than std::sprintf() when
    converting a number to a string. In fact sprintf() which should never
    be called at all in a modern application - due to the risk of
    overruning the buffer by calling the function.

    Of course snprintf() is not perfectly safe either, since a buffer
    overrun - while less likely - is still possible. With a string class
    object, however, there is no risk at all of a buffer overrun. Therefore
    using std::string for the number-to-string conversion is the best
    option; and std::string should be the default choice of for managing
    string values in a C++ program.

    Greg
    Greg, Jan 12, 2007
    #11
  12. Huyvtq

    Rolf Magnus Guest

    Greg wrote:

    >> snprintf can be used (if implemented) as safe version of sprintf

    >
    > In a conforming C++ implementation, std::snprintf() will always be
    > implemented.


    No, it won't.

    > The routine is declared in <cstdio>.


    Actually, that's even forbidden for a conforming implementation.
    Rolf Magnus, Jan 13, 2007
    #12
  13. Huyvtq

    Grizlyk Guest

    Greg wrote:

    > Of course snprintf() is not perfectly safe either, since a buffer
    > overrun - while less likely - is still possible.


    Do not understand - overrun or no? I suppose, data can be truncated by
    small buffer and buffer can not be extended while snprintf executing,
    but external memory can not be damaged. Yes?
    Grizlyk, Jan 14, 2007
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. makok
    Replies:
    1
    Views:
    6,750
    Anders Hellerup Madsen
    Feb 23, 2004
  2. Replies:
    5
    Views:
    511
    Pete Becker
    May 21, 2005
  3. Hako
    Replies:
    13
    Views:
    38,793
    Ron Adam
    Nov 10, 2005
  4. muss
    Replies:
    13
    Views:
    707
    Vladimir S. Oka
    Mar 27, 2006
  5. davidj411
    Replies:
    11
    Views:
    1,273
    Kam-Hung Soh
    May 29, 2008
Loading...

Share This Page