home *** CD-ROM | disk | FTP | other *** search
- /*********
- * Base10.C
- * by Leonard Zerman
- * Placed in the public domain by Tom Rettig Associates, 10/22/1990.
- *
- * Syntax: BASE10( <expN>, <expC> )
- * Return: <expN> decimal value of <base number>
- * -1 if invalid character in <base number>
- * Note : Maximum <base> is 36.
- * Minimum <base> is 2.
- * <base number> is character.
- * The largest return value is that of a long int.
- * Which is 2,147,483,647
- * MAX <base 36> = "ZIK0ZJ"
- * MAX <base 2 > = "11111111111111111111111111111111" (32 one's)
- * If overflow occurs "**********" is returned.
- ********/
-
- #include "trlib.h"
-
- TRTYPE base10() /* declare the base10 function */
- {
- static char lookup[] = {"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
- char *instr; /* pointer to the passed string */
- long i, j, k, base;
- long result = 0L; /* long int result for large numbers */
-
- if (PCOUNT == 2 && ISNUM(1) && ISCHAR(2)) /* check passed parms */
- {
- base = _parnl(1); /* assign vars */
- instr = _parc(2) ;
- }
- else
- {
- _retnl( ERRORNEGL ); /* return an error */
- return; /* return from program */
- }
-
- if ( base > 36L || base < 2L ) /* maximum base is 36 */
- { /* minimum base is 2 */
- _retnl( ERRORNEGL ); /* if first char is '0' */
- return; /* return error */
- }
-
- j = 1L;
-
- for (i = (long)_tr_strlen( instr ); i > 0L; i--)
- { /* process all characters */
- for (k = 0L; (k < base) && (toupper(instr[i -1L]) != lookup[k]); k++)
- ; /* search for character in lookup */
-
- if ( k < base ) /* k = the position of character or */
- { /* k = base if not found */
- result += ( k * j );
- j *= base; /* add (char position * base) */
- }
- else
- {
- _retnl( ERRORNEGL ); /* return an error */
- return;
- }
-
- }
-
- _retnl( result ); /* return result */
- }
- /* eof */
-