[comp.lang.c] Another version of itoa.c

jjsc@inf.rl.ac.uk (John Cullen) (07/05/90)

I am posting the following for a friend without Usenet access. He does receive
copies of all articles sent to this group, however any comments would best be
sent to him direct.

Thankyou for listening/reading :-)

John

---------Begin included message-------

From GORMAN_B@uk.ac.LANCSP.P1 Thu Jul  5 12:26:40 1990
Date:         Thu, 05 Jul 90 12:20:37
From: Barry Gorman  <GORMAN_B@uk.ac.LANCSP.P1>
Subject:      for comp.lang.c
To: jjsc@uk.ac.RL.INF

I saw a version of itoa on this list a few days ago; and I would like
to contribute my version. It works well on 32bit machines, but might
be improved for 16bit ones if short was used for "base".

Barry
.....

/** LIBRARY package with itoa, and aux functions _int_to_str & _u_int_to_str **/

/****** converts unsigned long integer to character string, any base 2-36 *****/
char *_u_int_to_str(number,base,buffer) unsigned long number,base; char *buffer;
/******************************************************************************/

#define table "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"            /* for lookup */

   {buffer+=33; *buffer=0;           /* skip to end of buffer and insert null */

    do {*--buffer=table[number%base]; number/=base;} while(number);/* convert */

    return(buffer);}       /* return value may not be at start of buffer area */

/******* converts signed long integer to character string, any base 2-36 ******/
char *_int_to_str(number,base,buffer) long number,base; char *buffer;
/******************************************************************************/

   {buffer=_u_int_to_str((number<0?-number:number), base, buffer); /* convert */

    if(number<0) *--buffer='-';          /* and put sign on front if negative */

    return(buffer);}      /* return value points at first character of result */

/*** library function itoa - converts signed long integer to base 10 string ***/
char *itoa(number) long number;/* returns pointer into a static array of char */
/******************************************************************************/

   {static char buffer[34]; return(_int_to_str(number, 10, buffer));}

/*********************************** the end **********************************/


---------End included message--------- 
===============================================================================
John Cullen                     || JANET : jjsc@uk.ac.rl.inf
System Support Group            || ARPA  : jjsc%inf.rl.ac.uk@nsfnet-relay.ac.uk
Informatics Department          || BITNET: jjsc%inf.rl.ac.uk@ukacrl
Rutherford Appleton Laboratory  || UUCP  : {...!mcsun}!ukc!rlinf!jjsc
Chilton, Didcot, Oxon. OX11 0QX || VOICE : +44 (0)235 821900 ext 6555  
===============================================================================