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

  1. /*
  2.  * File......: HEX2DEC.PRG
  3.  * Author....: Robert A. DiFalco
  4.  * Date......: $Date:   17 Aug 1991 15:32:56  $
  5.  * Revision..: $Revision:   1.3  $
  6.  * Log file..: $Logfile:   E:/nanfor/src/hex2dec.prv  $
  7.  * 
  8.  * This is an original work by Robert DiFalco and is placed in the
  9.  * public domain.
  10.  *
  11.  * Modification history:
  12.  * ---------------------
  13.  *
  14.  * $Log:   E:/nanfor/src/hex2dec.prv  $
  15.  * 
  16.  *    Rev 1.3   17 Aug 1991 15:32:56   GLENN
  17.  * Don Caton fixed some spelling errors in the doc
  18.  * 
  19.  *    Rev 1.2   15 Aug 1991 23:03:42   GLENN
  20.  * Forest Belt proofread/edited/cleaned up doc
  21.  * 
  22.  *    Rev 1.1   14 Jun 1991 19:51:58   GLENN
  23.  * Minor edit to file header
  24.  * 
  25.  *    Rev 1.0   01 Apr 1991 01:01:28   GLENN
  26.  * Nanforum Toolkit
  27.  *
  28.  */
  29.  
  30.  
  31. /*
  32.  * $DOC$
  33.  * $FUNCNAME$
  34.  *    FT_HEX2DEC()
  35.  *  $CATEGORY$
  36.  *    Conversion
  37.  * $ONELINER$
  38.  *    Convert a hex number to decimal
  39.  * $SYNTAX$
  40.  *    FT_HEX2DEC( <cHexNum> ) -> nDecNum
  41.  * $ARGUMENTS$
  42.  *    <cHexNum> is a character string representing a hex number.
  43.  * $RETURNS$
  44.  *    A decimal number.
  45.  * $DESCRIPTION$
  46.  *    Converts a hexadecimal number to a BASE 10 decimal number.
  47.  *    Useful for using FT_INT86().
  48.  * $EXAMPLES$
  49.  *    FT_INT86( HEX2DEC( "21" ), aRegs )
  50.  *
  51.  *    Converts 21h, the Dos Interrupt, to its decimal equivalent,
  52.  *    33, for use by FT_INT86().
  53.  * $END$
  54.  */
  55.  
  56. #define HEXTABLE "0123456789ABCDEF"
  57.  
  58. #ifdef FT_TEST
  59.   FUNCTION MAIN( cHexNum )
  60.      QOut( FT_HEX2DEC( cHexNum ) )
  61.      return ( nil )
  62. #endif
  63.  
  64. FUNCTION FT_HEX2DEC( cHexNum )
  65.    local n, nDec := 0, nHexPower := 1
  66.  
  67.    for n := len( cHexNum ) to 1 step -1
  68.       nDec += ( at( subs( upper(cHexNum), n, 1 ), HEXTABLE ) - 1 ) * nHexPower
  69.       nHexPower *= 16
  70.    next
  71.  
  72. RETURN nDec
  73.