Re: Converting int to string

From: r norman (rsn__at__comcast.net)
Date: 08/05/04

  • Next message: tom_usenet: "Re: Converting int to string"
    Date: Thu, 05 Aug 2004 07:34:32 -0400
    
    

    On 5 Aug 2004 01:17:26 -0700, pwipwi@ms66.hinet.net (Frank Yeh) wrote:

    >r norman <rsn_@_comcast.net> wrote in message news:<gea0h0tvgt500s46ihut2b8egk5h97mm7k@4ax.com>...
    >> On Tue, 3 Aug 2004 19:18:25 -0400, "David Mohandas"
    >> <david_mohandas@yahoo.com> wrote:
    >>
    >> >Is there a way in STL to convert integer data type to string object or do i
    >> >have to do itoa() to do that.
    >> >
    >>
    >> As an old C programmer, I find itoa (or even sprintf!) most
    >> convenient. However, here goes:
    >>
    >> std::ostringstream oss;
    >> int i = 123;
    >> oss << i;
    >> std::string s = oss.str();
    >
    > Although the standard method is highly portable, I still find that
    >using itoa is much faster. (even sprintf is faster than stringstream
    >convertion). So, If you love good performace, itoa will be a better
    >choice.

    I tend to agree with you. However, the fact is that I have also
    created innumerable bugs by using sprintf format values with the wrong
    type of value. The function itoa() is not standard C or C++ and you
    have to remember about _itoa or _ultoa or all the other versions. And
    also "modern" programming tends to use all sorts of data items beyond
    char, int, or float.

    If you love good programming techniques, you will use the standard
    methods. If you love good performance, then diligent attention to
    your program design will yield far more effective results than relying
    on a few little tricks.


  • Next message: tom_usenet: "Re: Converting int to string"

    Relevant Pages

    • Re: function conversion
      ... I read that i.e. itoa is not! ... You can see all the functions in the standard C library by reading the ... every programming aspect? ...
      (comp.lang.c)
    • Re: integer to characters
      ... but so did the itoa(). ... sprintf() is defined by the ... Standard, itoais not. ... The problem is easily averted by first checking your buffer size ...
      (alt.comp.lang.learn.c-cpp)
    • Re: Please help optimize (and standarize) this code...
      ... > where there's no itoa(). ... Do you suggest sprintf, or is there a standard, specific ... > Why don't you use sprintfto make up the format string? ... if you give sizeof the name of a real instance of the NameExt ...
      (comp.lang.c)
    • Re: how to represent binary code , hex code in c++ and display them on screen using prinf statement
      ... Borland C/C++ has in its library a function called itoa. ... the exact signature of the function. ... But there is no such standard ... > expects an unsigned int, so you'll need to convert or use the right type ...
      (comp.lang.cpp)
    • Re: [Clax86list] Feedback please (Linux, NASM code)
      ... Frank Kotler wrote: ... :(why the hell can't I find "itoa" in the man pages???) ... It was "sprintf" that allowed the buffer ... overflow in Nasm - had to replace it with "snprintf". ...
      (comp.lang.asm.x86)