; HEXTONUM.ASP  v1.00  Hex to decimal conversion utility
;*****************************************************************************
;*                                                                           *
;* HEXTONUM.ASP                                                              *
;* Copyright (C) 1993 Datastorm Technologies, Inc.                           *
;* All rights reserved.                                                      *
;*                                                                           *
;* This script prompts the user for a hex value and displays the decimal     * 
;* representation.                                                           *
;*                                                                           *
;* Author: Chuck Spohr                                                       *
;*                                                                           *
;* This ASPECT SCRIPT is intended only as a sample of ASPECT programming.    *
;* DATASTORM makes no warranty of any kind, express or implied, including    *
;* without limitation, any warranties of merchantability and/or fitness      *
;* for a particular purpose.  Use of this program is at your own risk.       *
;*****************************************************************************

;*****************************************************************************
;*                                                                           *
;* MAIN                                                                      *
;* The Main procedure draws the dialog box, prompts the user for the value   *                                   
;* to convert, calls the HexToNum procedure to make the conversion, then     *
;* displays the result and prompts again.                                    *
;*                                                                           *
;* Calls: HexToNum                                                           *
;* Modifies globals: None                                                    *
;*                                                                           *
;*****************************************************************************

proc main
   string HexStr = ""                     ; Initialize hex input string
   integer DecInt, Row = $ROW, Col = $COL ; Init dec result, save cursor loc
   
   set rxdata on                          ; Pause incoming data
   set keys on                            ; Force Aspect processing of keys
   vidsave 0                              ; Save current screen
   
   box 9 22 15 62 0                       ; Draw shadow of box
   box 8 20 14 60 79                      ; Draw box
   atsay 10 24 79 "Enter hex value to convert:"   ; Display prompt text
   
   while 1                                ; Begin infinite loop
      atget 10 52 15 4 HexStr DEFAULT     ; Get Hex value
      if failure                          ; If user pressed ESC
         exitwhile                        ; Exit the loop
      endif
      
      call HexToNum with HexStr &DecInt   ; Convert hex value

      atsay 12 37 79 "Decimal value:        "  ; Display result
      atsay 12 52 79 DecInt
   endwhile                               ; End loop

   vidrest 0                              ; Restore screen
   locate Row, Col                        ; and cursor location
endproc 

;*****************************************************************************
;*                                                                           *
;* HEXTONUM(string, integer)                                                 *
;* The HexToNum procedure parses the hex string, translates, and does the    *
;* arithmetic to find the resulting integer value.                           *
;*                                                                           *
;* Calls: None                                                               *
;* Called by: Main                                                           *
;* Modifies globals: None                                                    *
;*                                                                           *
;*****************************************************************************

proc HexToNum
strparm SourceStr                         ; Hex value parameter
intparm TargetInt                         ; Decimal value to be returned

   integer Len, Chr, Val = 0, Index = -1, Multiplier = 1
   
   TargetInt = 0                          ; Start at 0
   strlen SourceStr Len                   ; Find length of hex value
   Index = Len - 1                        ; We'll start at the last character
   
   while Index >= 0                       ; Loop until we reach the 1st char
      strpeek SourceStr Index Chr         ; Look at the next char in hex str
      if (Chr >= '0' && Chr <= '9')       ; Is it a numeral?
         Val = Chr - 48                   ; Convert its ASCII value
      elseif (Chr >= 'A' && Chr <= 'F')   ; Is it a letter?
         Val = Chr - 55                   ; Convert its ASCII value
      elseif (Chr >= 'a' && Chr <= 'f')
         Val = Chr - 87
      else                                ; Is it invalid?
         exitwhile                        ; Get out
      endif
      
      TargetInt = TargetInt + (Val * Multiplier) ; Add value to running total
      Multiplier = Multiplier * 16        ; Update the multiplier
      Index--                             ; Decrement character index
   endwhile                               ; End loop
endproc
