Subject: Re: CCL:small linux/c++ question.

From: Markku Laukkanen <[email-masked]>
Date: Thu, 14 Mar 2002 07:16:21 +0200 (EET)

And same in c++ & any base, no reverse needed :-)

	PKY

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

#include <string>

using namespace std;

#define MAX_SIZE_OF_OUTPUT_BUF 1024

char * convertToBase(int number, int base) {
  char buf[MAX_SIZE_OF_OUTPUT_BUF];
  char* tp = buf + sizeof buf;
  *--tp = '\0';
  
  bool neg = false;
  if (number < 0) {
    neg = true;
    number = -number;
  }
  
  int j = 0;
  do {
    if (j++ % 4 == 3) {
      ++j;
    }
    int num = number % base;
    if ((base <= 10) || num < 10) {
      *--tp = '0' + num;
    } else {
      char start = 'A' + base - 10;
      *--tp = start + num - base;
    }
    number /= base;
  } while (number);
  	
  char* sp = (char*)malloc(MAX_SIZE_OF_OUTPUT_BUF);
  if (neg) *sp++ = '-';
  
  strcpy(sp,tp);
  if (neg) {
    return --sp;
  } else {     return sp;
  }
}

string i2String(int value) {
  char* val = convertToBase(value,10);
  string retVal = val;
  free(val);
  return retVal;
}

On Wed, 13 Mar 2002, Mark Thompson wrote:

> 
> Dear Krzysztof,
> 
> I've appended an implementation that I've used in the past.  It creates the
> string in inverted order and reverses it at the end.  All the usual caveats
> about making sure the argument s[] is big enough apply...
> 
> Mark
> 
> =================================
> Mark Thompson, Ph.D.
> Planaria Software
> Seattle, WA.
> http://www.arguslab.com
> =================================
> 
> 
> void Itoa (int n, char s[])
> {
>   int   i, j, c, sign;
> 
>   if ((sign = n) < 0) {n = -n;}
> 
>   i = 0;
> 
>   do
>   {
>     s[i++] = (n % 10) + '0';
>   } while ((n /= 10) > 0);
> 
> 
>   if (sign < 0) {s[i++] = '-';}
> 
>   s[i] = '\0';
> 
>   for (i = 0, j = strlen(s) - 1;  i < j; i++, j--)
>   {
>     c    = s[i];
>     s[i] = s[j];
>     s[j] = c;
>   }
> }
> 
> 
> > -----Original Message-----
> > From: Computational Chemistry List [mailto:[email-masked]]On
> > Behalf Of Krzysztof Radacki
> > Sent: Wednesday, March 13, 2002 3:43 AM
> > To: CCL
> > Subject: CCL:small linux/c++ question.
> >
> >
> > Dear CCL'ers
> > I have a little bit out of topic question.
> > Currently I'm translating my program from Borland C++
> > to gcc and I can't find a function itoa in gcc (it
> > converts integer to string).
> > I would be quite happy if someone could explaine me how
> > to do it in gcc.
> >
> > MfG
> > Krzysztof
> >
> > p.s.
> > because it's realy out-of-topic maybe better if answer
> > would be addressed directly to my e-mail not to ccl.
> >
> >
> > --
> > Dr. K.Radacki
> > Department of Chemistry
> > Catalysis and Advanced Materials Section
> > Imperial College, London
> >
> > [email-masked] -- To Everybody  | [email-masked] --
> > To Admins
> > [email-masked] -- archive search    |    Gopher:
> > gopher.ccl.net 70
> > Ftp: ftp.ccl.net  |  WWW: http://www.ccl.net/chemistry/   | Jan:
> > [email-masked]
> >
> >
> >
> >
> >
> >
> 
> 
> -= This is automatically added to each message by mailing script =-
> [email-masked] -- To Everybody  | [email-masked] -- To Admins
> [email-masked] -- HELP CHEMISTRY or HELP SEARCH
> [email-masked] -- archive search    |    Gopher: gopher.ccl.net 70
> Ftp: ftp.ccl.net  |  WWW: http://www.ccl.net/chemistry/   | Jan: [email-masked]
> 
> 
> 
> 
>

---

From: Chas Simpson <[email-masked]>
Date: Thu, 14 Mar 2002 08:34:14 -0200 (GMT+2)

Hi

Just a suggestion, try stay away from malloc, free, realloc and calloc.
They're pre ANSI C++. Rather use the new and delete operators, ie:

char *sp = new char[MAX_SIZE_OF_OUTPUT_BUF];

and 

delete [] val;

Even better, try out the standard template library structures, such as
vectors or valarrays. It takes a bit of getting used to though.

Cheers

Chas

-- 
_______________________________________________
Chas Simpson
Computational Chemistry
University of Cape Town
South Africa
e-mail: [email-masked]
http://hydrogen.cem.uct.ac.za/compchem/
_______________________________________________

---

From: Rick Venable <[email-masked]>
Date: Thu, 14 Mar 2002 10:03:25 -0500

Gee, and it's a one liner in Fortran:

	character *80  str
	integer i

	write (str,'(I12)') i

> Ftp: ftp.ccl.net  |  WWW: http://www.ccl.net/chemistry/   | Jan: [email-masked]

=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
Rick Venable           29/500
FDA/CBER/OVRR Biophysics Lab
1401 Rockville Pike    HFM-419
Rockville, MD  20852-1448  U.S.A.
(301) 496-1905   [email-masked]
ALT email:  [email-masked]
=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=

---

From: "Michael Harig" <[email-masked]>
Date: Thu, 14 Mar 2002 18:14:24 +0100

> Gee, and it's a one liner in Fortran:
in c++ too (more or less):

#include <iomanip>
#include <sstream>

ostringstream    os;
int i=666;
os << i;

if you want the c++ string from the stream:

#include <string>
string aStr=os.str();

if you want a char * (tststs!) from the stream:

const char *aStr=os.str().c_str();        // i think there's a shorter
possibillity here, but i don't remember

if you want base, precision and width:

os << setbase(16) << setprecision(5) << setwidth(21) << i;

(o.k. valid base values are only 8,10 and 16, i guess)

regards,

    Michael

---

From: "David Konerding, Ph.D." <[email-masked]>
Date: Thu, 14 Mar 2002 07:04:54 -0800

>vectors or valarrays. It takes a bit of getting used to though.

This is getting rapidly off-topic, but...  original poster wanted an "itoa"
function like Borland C++ (a non-standard function, but since Borland C++ has
so many users, questions like this are common).

As others suggested, using snprintf is the simple C way to do it, highly portable,
although the difficulty is doing the the character buffer size allocation properly.

In C++, no vectors or valarrays are needed.  You can avoid the STL entirely and
use the standard C++ library.  This won't compile on many Linuces out-of-the-box
since they don't all support the full standard C++ library yet.    But it should work with
gcc-2.95.3/STLport or gcc-3.0.4/libstdc++.

#include <sstream>
#include <ostream>

int main(void) {
        double f=5.05;
        std::stringstream s;
        s << f;
        std::string s2;
        s2 = s.str();
        std::cout << s2;
}

---

From: Tom Sundius <[email-masked]>
Date: Thu, 14 Mar 2002 23:59:57 +0200 (EET)

> Gee, and it's a one liner in Fortran:

If you want a more general routine, it is not that easy. See R.K. Jones and T. Crabtree:
"Fortran tools for VAX/VMS and MS-DOS", p. 84 (itoa function). This book gives also
the Fortran equivalents of atoi, atof, ftoa, itoh and htoi. As you said it is of course
much easier to use internal files, but the executable programs are slightly smaller
if you are not using FORTRAN I/O (this is perhaps not an issue any longer, but the above
mentioned book was written in 1988, so...)

    Greetings,

       Tom S.

Tom Sundius
University of Helsinki, Dept of Physical Sciences   phone +358-9-191 50672
P.O.Box 64, FIN-00014 Helsinki, Finland             fax   +358-9-191 50610