[comp.lang.c] Source for "integer to ascii" and multiple file search-n-replace

mpledger@cti1.UUCP (Mark Pledger) (11/19/90)

I'm looking for a function that can convert an integer to an ascii string.
Microsoft has a function in its library called itoa() where you can specify
the radix result used in the ascii string.  I just want to convert integers
though.  I am looking for a fast implementation, without resorting to scanf()
or print() type functions.

Also, does anyone know of a program that can do multiple file search and 
replace (word or string specified on the command line).  For example if
you have a function called "dispaly_record()" and you want to change its
name to "print_record()", then calling this program would replace all
occurances of "display_record()" with "print_record()".


-- 
Sincerely,


Mark Pledger

--------------------------------------------------------------------------
CTI                              |              (703) 685-5434 [voice]
2121 Crystal Drive               |              (703) 685-7022 [fax]
Suite 103                        |              
Arlington, DC  22202             |              mpledger@cti.com
--------------------------------------------------------------------------

gordon@osiris.cso.uiuc.edu (John Gordon) (11/20/90)

mpledger@cti1.UUCP (Mark Pledger) writes:

>I'm looking for a function that can convert an integer to an ascii string.
>Microsoft has a function in its library called itoa() where you can specify
>the radix result used in the ascii string.  I just want to convert integers
>though.  I am looking for a fast implementation, without resorting to scanf()
>or print() type functions.

	Assuming: int integer;
	          char buffer[100];

		  sprintf(buffer, "%d", integer);  will do the job nicely.

epames@eos.ericsson.se (Michael Salmon) (11/20/90)

In article <318@cti1.UUCP> mpledger@cti1.UUCP (Mark Pledger) writes:
>Also, does anyone know of a program that can do multiple file search and 
>replace (word or string specified on the command line).  For example if
>you have a function called "dispaly_record()" and you want to change its
>name to "print_record()", then calling this program would replace all
>occurances of "display_record()" with "print_record()".
>
There are 2. Firstly of course there is sed and secondly there is gres.
I can't help you with source for either unfortunately. There are some
commercial sed's available (for I presume a PC as you talk of MS) and
Minix contains gres (I'm pretty sure). Perhaps somewhere like Austin
Code Works can help you.

Michael Salmon
L.M.Ericsson
Stockholm

carroll@cs.uiuc.edu (Alan M. Carroll) (11/20/90)

sprintf() is often times inadequate, or too bulky. This code will convert from
a long to any base 2..36.
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
char *
UnsignedLongToString(n,base)
     unsigned long n;
     unsigned int base;
{
  char *digit = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  static char LongToStringBuffer[34];	/* 32+nul+negative sign */
  char *s = LongToStringBuffer + 33; /* at most 33 characters in binary */

  *s = 0;			/* terminate */
  while (n)			/* something there */
    {
    *--s = digit[n % base];		/* store bottom digit */
    n /= base;			/* shift right */
    }
  if (*s == 0) *--s = '0';		/* in case nothing was put in string */
  return s;
}

char *
LongToString(n,base)
     long n;
     int base;
{
  char *s;

  if (n < 0)
    {
      s = UnsignedLongToString((unsigned long) -n, base);
      *--s = '-';
    }
  else s = UnsignedLongToString((unsigned long) n, base);
  return s;
}
/* ------------------------------------------------------------------------ */
main()
{
  printf("%s\n",LongToString(-1234567,10));
  printf("%s\n",UnsignedLongToString(7654321,10));
  printf("%s\n",LongToString(0x12345,16));
}

-- 
Alan M. Carroll                Barbara/Marilyn in '92 :
Epoch Development Team          + This time, why not choose the better halves?
CS Grad / U of Ill @ Urbana    ...{ucbvax,pur-ee,convex}!cs.uiuc.edu!carroll

mpledger@cti1.UUCP (Mark Pledger) (11/21/90)

>mpledger@cti1.UUCP (Mark Pledger) writes:

>>I'm looking for a function that can convert an integer to an ascii string.
>>Microsoft has a function in its library called itoa() where you can specify
>>the radix result used in the ascii string.  I just want to convert integers
>>though.  I am looking for a fast implementation, without resorting to scanf()
>>or print() type functions.
     ^

Maybe nobody is reading my question, but everyone keeps mailing me answers 
using sprintf() which is actually what I don't want.  I just noticed a typo
in my original question, but I think most people should be able to figure
that out since I also stated scanf().

I have had one good reply so far from Bill Poser (of Standford) who actually
had a function written in C that he so kindly passed along.

I will summerize any other replies received if anyone is interested.

 
-- 
Sincerely,


Mark Pledger

--------------------------------------------------------------------------
CTI                              |              (703) 685-5434 [voice]
2121 Crystal Drive               |              (703) 685-7022 [fax]
Suite 103                        |              
Arlington, DC  22202             |              mpledger@cti.com
--------------------------------------------------------------------------

scs@adam.mit.edu (Steve Summit) (11/27/90)

In article <324@cti1.UUCP> mpledger@cti1.UUCP (Mark Pledger) writes:
>Maybe nobody is reading my question, but everyone keeps mailing me answers 
>using sprintf() which is actually what I don't want.

I was one of those respondents.  Unfortunately, I did not notice
that you did not want to "resort to print or scanf type
functions" until after I had replied.  Let's look at why so many
people were quick to suggest the use of sprintf.

I mailed you a copy of the frequently-asked questions list.  It
says

	Q. How can I write itoa?
	A: Just use sprintf.

It does not say

	Q. How can I write itoa if I don't care about efficiency?

or

	A: Just use sprintf, unless you need it fast.

Sprintf simply is the recommended function, and it is "fast
enough" in the vast majority of applications.  It is the rare
program that can be significantly sped up with a special-purpose
itoa-type routine, and you gave us no indication of why your
program might be one of them.

I suppose I'll have to make the FAQ list explicit on this point.

                                            Steve Summit
                                            scs@adam.mit.edu

P.S. Before 37 people follow up with 37 examples of rare programs
which DO care about integer to ASCII speed, berating me for my
irresponsibility in apparently advocating inefficiency, let me
say that I have heard of those purported examples before, and in
any case I did not claim that they were nonexistent, merely that
Mark Pledger hadn't shown that his was one of them.

lfd@cbnewsm.att.com (leland.f.derbenwick) (11/28/90)

In article <1990Nov26.192218.29782@athena.mit.edu>, scs@adam.mit.edu (Steve Summit) writes:
> In article <324@cti1.UUCP> mpledger@cti1.UUCP (Mark Pledger) writes:
> >Maybe nobody is reading my question, but everyone keeps mailing me answers 
> >using sprintf() which is actually what I don't want.
...
> Sprintf simply is the recommended function, and it is "fast
> enough" in the vast majority of applications.  It is the rare
> program that can be significantly sped up with a special-purpose
> itoa-type routine, and you gave us no indication of why your
> program might be one of them.
...

I believe very strongly in coding systems to have good performance.
But here's a rule of thumb for optimizing code:

    If you can't say very clearly _why_ you need a faster version
    of a particular routine, then you probably don't need it.

In the cases I can think of (implementation on bare hardware, with no
sprintf available; or absolutely performance-critical and the overall
algorithm can't be improved instead), knowing _why_ you need it would
imply enough knowledge to be able to code it yourself, easily.  (Well,
maybe if it were a homework assignment and the professor didn't allow
you to use sprintf! :-)    Anyway, if it's truly performance-critical,
you probably need a function tuned to the application: fixed-length
strings, or particular distributions of input, or only unsigned, or if
all else fails maybe even assembly language.  So generic code probably
wouldn't do.

 -- Speaking strictly for myself,
 --   Lee Derbenwick, AT&T Bell Laboratories, Warren, NJ
 --   lfd@cbnewsm.ATT.COM  or  <wherever>!att!cbnewsm!lfd

rh@smds.UUCP (Richard Harter) (11/28/90)

In article <1990Nov27.224111.24038@cbnewsm.att.com>, lfd@cbnewsm.att.com (leland.f.derbenwick) writes:
> In article <1990Nov26.192218.29782@athena.mit.edu>, scs@adam.mit.edu (Steve Summit) writes:
> > In article <324@cti1.UUCP> mpledger@cti1.UUCP (Mark Pledger) writes:
> > >Maybe nobody is reading my question, but everyone keeps mailing me answers 
> > >using sprintf() which is actually what I don't want.

> > Sprintf simply is the recommended function, and it is "fast
> > enough" in the vast majority of applications...

	... very reasonable remarks about "handrolling your own"
	deleted.

Everyone seems to be overlooking that there is a perfectly good reason
why one might want to avoid sprintf -- if the program does not use the
printf family the relevant library routines are not loaded and the
executables are much smaller.  Sometimes this is a consideration.
-- 
Richard Harter, Software Maintenance and Development Systems, Inc.
Net address: jjmhome!smds!rh Phone: 508-369-7398 
US Mail: SMDS Inc., PO Box 555, Concord MA 01742
This sentence no verb.  This sentence short.  This signature done.

peter@ficc.ferranti.com (Peter da Silva) (11/29/90)

Another thing to consider: if sprintf() is too slow you're probably doing
too many conversions. I've run into this in the past in a couple of places
(my browser utility on the Amiga, where I'm displaying file dates as the
user scrolls the window up and down, for example) and the solution is NOT
to hand-code some gibberish but to do something like cache the text and
only re-convert it when the value changes.

(yep, the old "use a better algorithm" solution)
-- 
Peter da Silva.   `-_-'
+1 713 274 5180.   'U`
peter@ferranti.com