home *** CD-ROM | disk | FTP | other *** search
/ Power CD-ROM!! 7 / POWERCD7.ISO / prgmming / clipper / gtmath07.prg < prev    next >
Text File  |  1993-10-14  |  2KB  |  64 lines

  1. /*
  2.  * File......: GTMATH07
  3.  * Author....: Philip Ide
  4.  * BBS.......: Dark Knight Returns
  5.  * Net/Node..:
  6.  * User Name.: Philip Ide
  7.  * Date......: $Date$
  8.  * Revision..: $Revision$
  9.  * Log file..: $Logfile$
  10.  *
  11.  * This is an original work by Philip Ide and is placed in the
  12.  * public domain.
  13.  *
  14.  * Modification history:
  15.  * ---------------------
  16.  *
  17.  * $Log$
  18.  *
  19.  */
  20.  
  21. /*  $DOC$
  22.  *  $FUNCNAME$
  23.  *      GT_B36TOD()
  24.  *  $CATEGORY$
  25.  *      Maths
  26.  *  $ONELINER$
  27.  *      Turns a Base36 number into a decimal
  28.  *  $SYNTAX$
  29.  *      GT_B36ToD(<cB36Num>) --> <nDec>
  30.  *  $ARGUMENTS$
  31.  *      <cB36Num> is a character string representing a Base36 number
  32.  *                This need to be in upper case.
  33.  *  $RETURNS$
  34.  *      <nDec>    is a decimal integer
  35.  *  $DESCRIPTION$
  36.  *      This function provides a method of unpacking numbers stored in
  37.  *      Base36 format to decimal integers.  Base36 allows large numbers
  38.  *      to be stored using very few bytes. Four bytes could hold a number
  39.  *      as high as 1,679,615.  Useful for lookup table ID's.
  40.  *
  41.  *  $EXAMPLES$
  42.  *      ? GT_B36ToD('ZZZZ')       // 1679615
  43.  *
  44.  *      ? GT_B36ToD('11')         // 37
  45.  *  $SEEALSO$
  46.  *      GT_DTOB36()
  47.  *  $INCLUDE$
  48.  *
  49.  *  $END$
  50.  */
  51.  
  52. STATIC B36 := '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  53.  
  54. FUNCTION GT_B36toD( n )    // n=Base36 number
  55. LOCAL x, y := 0, z, a := 1
  56. FOR x := 1 TO Len(n)                        // once per byte
  57.     z := AT(SubStr(n,-x,1),B36)-1           // get 1 byte, start at far end
  58.     IF x > 1                                // miss this the first time...
  59.        a *= 36                              // exponentiate by 36
  60.     Endif
  61.     y += (a*z)                              // exponentiate the byte,
  62. Next                                        // add to result.
  63. Return (y)
  64.