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

  1. /*
  2.  * File......: GTMATH04
  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_HTOD()
  24.  *  $CATEGORY$
  25.  *      Maths
  26.  *  $ONELINER$
  27.  *      Turns a Hex string into a decimal integer
  28.  *  $SYNTAX$
  29.  *      GT_HtoD(<cHex>) --> <nDec>
  30.  *  $ARGUMENTS$
  31.  *      <cHex>  is a hexadecimal number, held as a string
  32.  *  $RETURNS$
  33.  *      <nDec>  is a decimal integer
  34.  *  $DESCRIPTION$
  35.  *      This function allows hex numbers to be converted into decimals.
  36.  *      This allows calculations to be applied on hex values
  37.  *      (e.g. incrementing).
  38.  *  $EXAMPLES$
  39.  *      ? GT_HtoD('FF')       // 255
  40.  *
  41.  *      ? GT_HtoD('F')+1      // 16
  42.  *
  43.  *      ? GT_HtoD('A')*2      // 20
  44.  *
  45.  *  $SEEALSO$
  46.  *      GT_DTOH()
  47.  *  $INCLUDE$
  48.  *
  49.  *  $END$
  50.  */
  51.  
  52. STATIC hex := '0123456789ABCDEF'
  53.  
  54. FUNCTION GT_HtoD( n )    // n=hexnum
  55. LOCAL x, y := 0, z, a := 1
  56. FOR x := 1 TO Len(n)                        // once per byte
  57.     z := AT(SubStr(n,-x,1),hex)-1           // get 1 byte, start at far end
  58.     IF x > 1                                // miss this the first time...
  59.        a *= 16                              // exponentiate by 16
  60.     Endif
  61.     y += (a*z)                              // exponentiate the byte,
  62. Next                                        //  add to result.
  63. Return (y)
  64.