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

  1. /*
  2.  * File......: GTMATH02
  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_DTOBIN()
  24.  *  $CATEGORY$
  25.  *      Maths
  26.  *  $ONELINER$
  27.  *      Turns a Decimal number into a binary string
  28.  *  $SYNTAX$
  29.  *      GT_DtoBin( <n> ) --> <cBinary>
  30.  *  $ARGUMENTS$
  31.  *      <n>  is any decimal integer
  32.  *  $RETURNS$
  33.  *      <cBinary> is a string representing a binary number
  34.  *  $DESCRIPTION$
  35.  *      This function turns a decimal integer into a binary string.
  36.  *      This can be used for encryption, bit twiddling,
  37.  *      bit stripping etc.
  38.  *  $EXAMPLES$
  39.  *      ? GT_DtoBin(255)     // '11111111'
  40.  *      ? GT_DtoBin(15)      // '00001111'
  41.  *  $SEEALSO$
  42.  *      GT_BTOD() GT_BINARY()
  43.  *  $INCLUDE$
  44.  *      gt_lib.ch
  45.  *  $END$
  46.  */
  47.  
  48. #xcommand DEFAULT <a> TO <y> [,<b> TO <z>] ;
  49.      => <a> := IF(<a> == NIL, <y>, <a>) ;
  50.      [; <b> := IF(<b> == NIL, <z>, <b>)]
  51.  
  52. STATIC bin := '01'
  53.  
  54. Function GT_DToBin( n )  // n = Decimal Number
  55. LOCAL retval := "", x, y
  56.  
  57. DEFAULT n TO 0
  58.  
  59. While (n>0)
  60.    x := n%2
  61.    n := Int(n/2)
  62.    retval := SubStr(bin, x+1, 1)+retval
  63. End
  64. Return (If(Len(retval)<1,"0",retval))
  65.