home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1993 #2 / Image.iso / clipper / nettos11.zip / MISC / FNBIN2I.PRG < prev    next >
Text File  |  1993-02-23  |  3KB  |  133 lines

  1. /*
  2.  * File......: FNBIN2I.PRG
  3.  * Author....: Steve Larsen
  4.  * CIS ID....: 76370,1532
  5.  * Date......: $Date$
  6.  * Revision..: $Revision$
  7.  * Log file..: $Logfile$
  8.  *
  9.  * This is an original work by Steve Larsen and is placed in the
  10.  * public domain.
  11.  *
  12.  * Modification history:
  13.  * ---------------------
  14.  *
  15.  * $Log$
  16.  *
  17.  */
  18.  
  19. #include "netto.ch"
  20.  
  21.  
  22. /*  $DOC$
  23.  *  $FUNCNAME$
  24.  *      fn_Bin2I()
  25.  *  $CATEGORY$
  26.  *      Miscellaneous
  27.  *  $ONELINER$
  28.  *      Convert a binary string to numeric
  29.  *  $SYNTAX$
  30.  *
  31.  *      fn_Bin2I( <cBinStr> ) -> nValue
  32.  *
  33.  *  $ARGUMENTS$
  34.  *
  35.  *      <cBinStr> is a string of bytes representing a number.
  36.  *
  37.  *  $RETURNS$
  38.  *
  39.  *      The decimal value of <cBinStr>.
  40.  *
  41.  *  $DESCRIPTION$
  42.  *
  43.  *      Internally, all information is stored as a series of bytes.  When
  44.  *      performing a direct read from memory, a character string will be
  45.  *      returned.  Use this function to convert that string to its Clipper
  46.  *      numeric equivalent.
  47.  *
  48.  *      This function only returns integer values.
  49.  *
  50.  *  $EXAMPLES$
  51.  *
  52.  *      /* convert a string containing CHR(0)+CHR(0)+CHR(222)+CHR(175) */
  53.  *
  54.  *      ? fn_Bin2I( "  ▐»" )                            // 57007
  55.  *
  56.  *  $SEEALSO$
  57.  *      fn_I2Bin() fn_PeekStr() fn_PokeStr()
  58.  *  $INCLUDE$
  59.  *
  60.  *  $END$
  61.  */
  62.  
  63. #include "netto.ch"
  64.  
  65. FUNCTION fn_Bin2I( cStr )
  66.     LOCAL i, nRetVal := 0
  67.     FOR i := 1 TO LEN( cStr )
  68.         nRetVal += BYTE2I( SUBST( cStr, i, 1 ) ) * 256^( LEN( cStr ) - i )
  69.     NEXT
  70. RETURN INT( nRetVal )
  71.  
  72. /*  $DOC$
  73.  *  $FUNCNAME$
  74.  *      fn_I2Bin()
  75.  *  $CATEGORY$
  76.  *      Miscellaneous
  77.  *  $ONELINER$
  78.  *      Convert an integer to binary string
  79.  *  $SYNTAX$
  80.  *
  81.  *      fn_I2Bin( <nValue>, [ <nLen> ] ) -> cBinStr
  82.  *
  83.  *  $ARGUMENTS$
  84.  *
  85.  *      <nValue> is the integer to convert.  Only positive integers may
  86.  *      be converted.
  87.  *
  88.  *      <nLen> is the desired resultant string length.  If omitted, it
  89.  *      defaults to the length required to represent <nValue>.  If <nLen>
  90.  *      is larger than the length required to represent <nValue>, cBinStr
  91.  *      is left-padded with CHR(0)'s to fill it out to the desired length.
  92.  *
  93.  *  $RETURNS$
  94.  *
  95.  *      A binary string representing the value passed.
  96.  *
  97.  *  $DESCRIPTION$
  98.  *
  99.  *      This function converts a decimal value to it binary equivalent,
  100.  *      and is useful for doing a direct memory translation.
  101.  *
  102.  *  $EXAMPLES$
  103.  *
  104.  *      ? fn_I2Bin( 57007 )                            // "▐»"  
  105.  *
  106.  *      ? fn_I2Bin( 57007, 4 )                        // "  ▐»"  
  107.  *
  108.  *  $SEEALSO$
  109.  *      fn_Bin2I() fn_PeekStr() fn_PokeStr()
  110.  *  $INCLUDE$
  111.  *
  112.  *  $END$
  113.  */
  114.  
  115. FUNCTION fn_I2Bin( nValue, nLen )
  116.     LOCAL cStr := "", nResult := 0, i, nTemp
  117.  
  118.     IF nLen == NIL                                   // determine minimum str. len
  119.         nLen := 0
  120.         WHILE ( nValue % (256^nLen++) ) < nValue  // each byte represents 256^n
  121.         END
  122.         --nLen
  123.     ENDIF
  124.  
  125.     FOR i := 1 TO nLen                          // build string by subtracting
  126.         nTemp   := INT( nValue / 256^( nLen - i ) )  // (byte)*(256^n) for 
  127.         cStr    += CHR( nTemp )                                // nLen bytes
  128.         nValue  -= nTemp * 256^( nLen - i )
  129.     NEXT
  130.  
  131. RETURN( cStr )
  132.  
  133.