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

  1. /*
  2.  * File......: GTMATH06
  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_DTOB36()
  24.  *  $CATEGORY$
  25.  *      Maths
  26.  *  $ONELINER$
  27.  *      Turns a decimal integer into a Base36 number
  28.  *  $SYNTAX$
  29.  *      GT_DtoB36(<n> [,nLength]) --> <cB36Num>
  30.  *  $ARGUMENTS$
  31.  *      <n>       is a decimal integer
  32.  *
  33.  *      <nLength> is a ^bMINIMUM^b length string to return.
  34.  *                If passed, this will cause the return value to be
  35.  *                padded with leading zero's to the length specified.
  36.  *                If the string is longer than <nLength>, it is not truncated.
  37.  *
  38.  *                If omitted, the return value is 'trimmed'.
  39.  *  $RETURNS$
  40.  *      <cB36Num> is a character string representing a Base36 number.
  41.  *  $DESCRIPTION$
  42.  *      This turns a decimal number into a Base36 number, represented by a
  43.  *      string.  Large numbers can be stored in a very few bytes, so this is
  44.  *      ideal for lookup table ID's, file offsets etc.
  45.  *  $EXAMPLES$
  46.  *      ? GT_DtoB36(12)        // 'C'
  47.  *
  48.  *      ? GT_DtoB36(1679615)   // 'ZZZZ'
  49.  *
  50.  *      ? GT_DtoB36(1679615,5) // '0ZZZZ'
  51.  *
  52.  *      ? GT_DtoB36(1679615,3) // 'ZZZZ'
  53.  *  $SEEALSO$
  54.  *      GT_B36TOD()
  55.  *  $INCLUDE$
  56.  *
  57.  *  $END$
  58.  */
  59.  
  60. #xcommand DEFAULT <a> TO <y> [,<b> TO <z>] ;
  61.      => <a> := IF(<a> == NIL, <y>, <a>) ;
  62.      [; <b> := IF(<b> == NIL, <z>, <b>)]
  63.  
  64. STATIC B36 := '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  65.  
  66. FUNCTION GT_DtoB36(n,l) // n=Decnum, l=length of Base36 string to return
  67. LOCAL retval := "", x, y
  68.  
  69. DEFAULT l TO 4
  70. While (n>0)                                 // while we got a number...
  71.    x := n%36                                // get modulus
  72.    n := Int(n/36)                           // remove an exponent
  73.    retval := SubStr(B36, x+1, 1)+retval     // add result to return string
  74. End
  75. l := Max(l,Len(retval))
  76. Return (SubStr(Replicate('0',l)+retval,-l)) // ret padded with leading zero's
  77.