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

  1. /*
  2.  * File......: GTMATH03
  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_BTOD()
  24.  *  $CATEGORY$
  25.  *      Maths
  26.  *  $ONELINER$
  27.  *      Convert a binary string into a decimal number
  28.  *  $SYNTAX$
  29.  *      GT_BtoD(<cBin>) --> <nDec>
  30.  *  $ARGUMENTS$
  31.  *      <cBin>   is a binary number held as a character string.
  32.  *  $RETURNS$
  33.  *      <nDec>   is a decimal integer
  34.  *  $DESCRIPTION$
  35.  *      This function takes a stream of '1's and '0's representing a binary
  36.  *      number, and converts them to a decimal.
  37.  *  $EXAMPLES$
  38.  *      ? GT_BtoD('11111111')        // 255
  39.  *
  40.  *      ? GT_BtoD('1011')            // 11
  41.  *  $SEEALSO$
  42.  *      GT_DTOB()  GT_BINARY()
  43.  *  $INCLUDE$
  44.  *
  45.  *  $END$
  46.  */
  47.  
  48. STATIC bin := '01'
  49.  
  50. Function GT_BtoD( n )
  51. LOCAL x, y := 0, z, a := 1
  52. FOR x := 1 TO Len(n)                        // once per byte
  53.     z := AT(SubStr(n,-x,1),bin)-1           // get 1 byte, start at far end
  54.     IF x > 1                                // miss this the first time...
  55.        a *= 2                               // exponentiate by 2
  56.     Endif
  57.     y += (a*z)                              // exponentiate the byte,
  58. Next                                        // add to result.
  59. Return (y)
  60.