int to string conversion

Gottfried Ganßauge g.ganssauge at 01019freenet.de
Sun Feb 13 11:04:53 GMT 2000


  -----Ursprüngliche Nachricht----- 
  Von: Martin Wiebusch 
  An: kdevelop at barney.cs.uni-potsdam.de 
  Gesendet: Samstag, 12. Februar 2000 13:49
  Betreff: Re: int to string conversion


  Jerry L Kreps wrote:
  > 
  > Martin Wiebusch wrote:
  > >
  > > Hi,
  > > i was just wondering whether there is a function to convert integer
  > > values to strings in any of the libaries. I already tried itoa, Itoa and
  > > IntToStr. I also noticed there ist an Itoa function defined in the
  > > Integer.h header. But I don't really need an Integer class, just a
  > > little function to convert usual int variables into strings.
  > 
  > You can use I/O manipulators (page 259, Practical C++
  > programming)

  Thankx, but I knew that Numbers are converted to strings when you write
  them on stdout with cout << ...
  What I want to do is writing that what in your examples appears on the
  screen to a variable of the type "string".
  Tilo Riemer just wrote me, that I can use "sprintf". But then I have to
  guess how many digits number will have. So it would be great if there
Don't you think it is so difficult to estimate some upper limit of characters needed?
For a 32-Bit int you need 9+1+1 chars (9 digits + sign + terminating NUL), for a 64 Bit int there would be some other figure but in any case the overhead for the conversion would be drastically less than using something dynamic.
  was something like "sprintf" for instances of the string class. Any
  idea?
how about:

std::string itoa (int i)
{
    char tmp [<the constant calculated above>];
 
    sprintf (tmp, "%d", i);
    return (std::string (tmp));
}

Gottfried Ganßauge

M.P.W.LASEC Software GmbH
Potsdamer Straße 99
10785 Berlin
http://www.mpwsoftware.de
ganssauge at mpwsoftware.de
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.kde.org/pipermail/kdevelop/attachments/20000213/94294515/attachment.html>


More information about the KDevelop mailing list