home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / nfsrc21.zip / DECTOBIN.PRG < prev    next >
Text File  |  1991-08-16  |  2KB  |  73 lines

  1. /*
  2.  * File......: DECTOBIN.PRG
  3.  * Author....: Greg Lief
  4.  * CIS ID....: 72460,1760
  5.  * Date......: $Date:   15 Aug 1991 23:03:22  $
  6.  * Revision..: $Revision:   1.2  $
  7.  * Log file..: $Logfile:   E:/nanfor/src/dectobin.prv  $
  8.  *
  9.  * This function is an original work by Mr. Grump and is placed in the
  10.  * public domain.
  11.  *
  12.  * Modification history:
  13.  * ---------------------
  14.  *
  15.  * $Log:   E:/nanfor/src/dectobin.prv  $
  16.  * 
  17.  *    Rev 1.2   15 Aug 1991 23:03:22   GLENN
  18.  * Forest Belt proofread/edited/cleaned up doc
  19.  * 
  20.  *    Rev 1.1   14 Jun 1991 19:51:30   GLENN
  21.  * Minor edit to file header
  22.  * 
  23.  *    Rev 1.0   01 Apr 1991 01:01:06   GLENN
  24.  * Nanforum Toolkit
  25.  *
  26.  */
  27.  
  28. /*  $DOC$
  29.  *  $FUNCNAME$
  30.  *     FT_DEC2BIN()
  31.  *  $CATEGORY$
  32.  *     Conversion
  33.  *  $ONELINER$
  34.  *     Convert decimal to binary
  35.  *  $SYNTAX$
  36.  *     FT_DEC2BIN( <nNum> ) -> cBinaryNumber
  37.  *  $ARGUMENTS$
  38.  *     <nNum> is the numeric expression to be converted.
  39.  *  $RETURNS$
  40.  *     A character string representing <nNum> in binary format.
  41.  *  $DESCRIPTION$
  42.  *     This function can be used in conjunction with any bit-wise
  43.  *     operations.
  44.  *  $EXAMPLES$
  45.  *     QOut( FT_DEC2BIN(255) )        // "11111111"
  46.  *     QOut( FT_DEC2BIN(2) )          // "00000010"
  47.  *  $END$
  48.  */
  49.  
  50. #ifdef FT_TEST
  51.  
  52. FUNCTION MAIN
  53. LOCAL X
  54. FOR X = 1 TO 255
  55.    QOUT( FT_DEC2BIN( x ))
  56. next
  57. return nil
  58.  
  59. #endif
  60.  
  61. function FT_DEC2BIN(x)
  62. local i, buffer := { '0', '0', '0', '0', '0', '0', '0', '0' }
  63. for i = 8 to 1 step -1
  64.   if x >= 2 ^ (i - 1)
  65.      x -= 2 ^ (i - 1)
  66.      buffer[9 - i] = '1'
  67.   endif
  68. next
  69. return ( buffer[1] + buffer[2] + buffer[3] + buffer[4] + ;
  70.          buffer[5] + buffer[6] + buffer[7] + buffer[8] )
  71.  
  72. * end of file: dectobin.prg
  73.