home *** CD-ROM | disk | FTP | other *** search
- /*********
- * base.c by Leonard Zerman
- * Placed in the public domain by Tom Rettig Associates, 10/22/1990.
- *
- * Syntax: BASE( <expN>, <expN> )
- * Return: <expC> value of <decimal number> in the mathematical
- * format of <base>. Both parameters are <expN>. Maximum
- * <base> is 36, Minimum <base> is 2.
- * Note : Changes negative number to positive.
- ********/
-
- #include "trlib.h"
-
- TRTYPE base() /* declare the base function */
- {
-
- static char funcname[] = {"base"}; /* program name for errors */
- static char lookup[] = {"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
- static char result[33];
- int i, j;
- long dec, base;
-
- if (PCOUNT == 2 && ISNUM(1) && ISNUM(2)) /* get parms from caller */
- {
- base = _parnl(1) ;
- dec = _parnl(2);
- }
- else
- {
-
- _retc( _tr_errmsgs(funcname, E_SYNTAX) ); /* return an error */
- return; /* return from program */
- }
-
- if ( base > 36 || base < 2 || dec > TWO_BILLION ) /* catch errors */
- {
- _retc( _tr_errmsgs(funcname, E_SYNTAX) ); /* return an error */
- return; /* return from program */
- }
-
- dec = ABS( dec ); /* take absolute value */
- i = j = 0; /* initialize vars */
-
- do
- {
- i = (int)( dec % base ); /* MOD to get look-up value */
- result[j++] = lookup[i]; /* assign look-up char to result */
- dec /= base; /* decrement value dec by base */
-
- } while ( dec > 0 ); /* until no decimal value left */
-
- result[j] = NULLC; /* terminate string with NULL char */
-
- for (i = 0, j = _tr_strlen( result ) - 1; i < j; i++, j--)
- {
- result[i] ^= result[j]; /* reverse the string */
- result[j] ^= result[i]; /* with the two chars */
- result[i] ^= result[j]; /* using XOR swap */
- }
-
- _retc(result); /* return the result */
- }
-