!###########################################################################
! PROGRAM CONVRT        This program will convert any input (within range)
!                       to HEX, BINARY, ASCII, DECIMAL, RAD50, and OCTAL
!                       values. Directions are provided during run-time.
!
! by DAVE HEYLIGER - AMUS STAFF
!
! last update: 09-11-85
!###########################################################################

THE'MAPS:
!          Defining all variables used in CONVRT
!__________________________________________________________________________

       MAP1 OP,S,16,""
       MAP1 RAD'GUYS,S,40,"#ABCDEFGHIJKLMNOPQRSTUVWXYZ$.?0123456789"
       MAP1 HEX'GUYS,S,16,"0123456789ABCDEF"
       MAP1 OCT'GUYS,S,8,"01234567"
       MAP1 DECIMAL,F                          !These are used for general
       MAP1 DEC'SAV,F
       MAP1 RAD50,S,6,""                       !conversion equations.
       MAP1 DIGIT,S,4,""
       MAP1 CHAR'STRING,S,3,""
       MAP1 CHAR'1,S,1,""                      !The individual characters as
       MAP1 CHAR'2,S,1,""                      !they are found.
       MAP1 CHAR'3,S,1,""
       MAP1 I,F                                !Various counters etc.
       MAP1 J,F
       MAP1 CURSOR,F
       MAP1 FLAG,F
       MAP1 HEX'VAL,S,4
       MAP1 OCT'VAL,S,6
       MAP1 BIN'VAL,S,16
       MAP1 ASC'VAL,S,4
       MAP1 TMP'VAL,F
       MAP1 BLANK,S,25,"                         "
       MAP1 BLANK2,S,50,"                                                  "
       MAP1 CNT,S,1,""

THE'TOP:
!       Prints out the 'screen format' and that's about it
!___________________________________________________________

       PRINT TAB(-1,0)

       ! Print the conversion template...

       ? TAB(8,29) "AMUS CONVERSION CALCULATOR"
       ? TAB(10,29) "HEX" TAB(11,29) "BINARY" TAB(12,29) "ASCII"
       ? TAB(13,29) "DECIMAL" TAB(14,29) "RAD50" TAB(15,29) "OCTAL"

START:  CURSOR = 0
START1:
!       Consider this the 'clean screen' situation.
!_____________________________________________________________________

       OP = ""
       ? TAB(20,20) "ENTER [CR] TO MOVE CURSOR TO APPROPRIATE BASE"
       ? TAB(21,21) "AND THEN ENTER IN WHAT YOU WANT CONVERTED."
       GOTO MOVE'IT     ! (MOVE'IT) simply moves the cursor to every base.

GET'IT:
!       The logic here is as follows: if the user enters a decimal
!       number, then the decimal number is used to calculate all of
!       the equivalent numbers in every other base. If the user does
!       NOT enter a decimal number, then the number (whatever base) is
!       converted to decimal, then this decimal number is used just as
!       if the user entered a decimal number to begin with.
!______________________________________________________________________

       INPUT"", OP : IF CURSOR <> 3 THEN OP = UCS(OP)   !NOT decimal number
       IF OP = "" THEN GOTO MOVE'IT                     !Moves to next base
       IF CURSOR = 4 THEN FLAG = 0 ELSE FLAG = 1        !A '0' means decimal
       ON CURSOR GOTO H'D, B'D, A'D, D'D, R'D, O'D      !Change base to dec.

H'D:
!       Converts HEX to decimal
!_______________________________

 DECIMAL = 0 : J = 0
 FOR I = LEN(OP) TO 1 STEP -1
   J = J + 1
   DECIMAL = DECIMAL +((INSTR(1,HEX'GUYS,(MID$(OP,J,1)))-1) *(16**(I-1)))
 NEXT I
 GOTO D'D

B'D:
!       Converts binary to decimal
!__________________________________

 DECIMAL = 0 : J = 0
 FOR I = LEN(OP) TO 1 STEP -1
    J = J + 1
    IF MID$(OP,J,1) = "1" THEN DECIMAL = DECIMAL + 2**(I-1)
 NEXT I
 GOTO D'D

A'D:
!       Converts a SINGLE character to decimal ASCII value.
!       If it is THREE characters, then it's a job for RAD50.
!___________________________________________________________

  IF LEN(OP) > 1 THEN GOTO RAD'50
  DECIMAL = ASC (OP)
  GOTO D'D

R'D:
!       Converts a RAD50 number into the three characters and only
!       the RAD50 number and these characters are displayed.
!___________________________________________________________________

 IF OP = "177777" THEN DIGIT = ":80" : GOTO P'D
 RAD50 = RIGHT$(("000000"+OP),6)
 DECIMAL = 0
 I = 1
 J = 5
II:
 DIGIT = MID$(RAD50,I,1)
 Y = VAL(DIGIT) * 8 ** J
 DECIMAL = DECIMAL + Y
 J = J - 1
 I = I + 1
 IF J >= 0 THEN GOTO II

TO'DECIMAL:
       DIGIT = ""
       DIGIT = MID$(RAD'GUYS,(INT(DECIMAL/1600)+1),1)
       DECIMAL = DECIMAL - (INT(DECIMAL/1600)*1600)
       DIGIT = DIGIT + MID$(RAD'GUYS,(INT(DECIMAL/40)+1),1)
       DECIMAL = DECIMAL - (INT(DECIMAL/40)*40)
       DIGIT = DIGIT + MID$(RAD'GUYS,DECIMAL+1,1)
P'D:    PRINT TAB(12,40) DIGIT
       GOTO CLEAN'UP

O'D:
!       Converts an octal number to the decimal equivalent
!_________________________________________________________

 J = 0 : DECIMAL = 0
 FOR I = LEN(OP) TO 1 STEP -1
    J = J + 1
    DECIMAL = DECIMAL + VAL(MID$(OP,J,1))*(8**(I-1))
 NEXT I

D'D:
!       Takes the decimal value entered (or calculated above) and
!       finds the values for each possible base
!________________________________________________________________

 IF FLAG = 0 THEN DECIMAL = VAL(OP)
 DEC'SAV = DECIMAL

TO'HEX:
 HEX'VAL = ""
 FOR I = 4 TO 1 STEP -1
   TMP'VAL = INT(DECIMAL / 16**(I-1))
   DECIMAL = DECIMAL - TMP'VAL*16**(I-1)
   HEX'VAL = HEX'VAL + MID$(HEX'GUYS,(TMP'VAL+1),1)
 NEXT I

TO'OCT:
 OCT'VAL = "" : DECIMAL = DEC'SAV
 FOR I = 6 TO 1 STEP -1
   TMP'VAL = INT(DECIMAL / 8**(I-1))
   DECIMAL = DECIMAL - TMP'VAL*8**(I-1)
   OCT'VAL = OCT'VAL + MID$(OCT'GUYS,(TMP'VAL+1),1)
 NEXT I

TO'BIN:
 BIN'VAL = "" : DECIMAL = DEC'SAV
 FOR I = 16 TO 1 STEP -1
   TMP'VAL = INT(DECIMAL / 2**(I-1))
   DECIMAL = DECIMAL - TMP'VAL*2**(I-1)
   IF TMP'VAL = 1 THEN BIN'VAL = BIN'VAL + "1" &
                  ELSE BIN'VAL = BIN'VAL + "0"
 NEXT I

TO'ASCII:
 IF DEC'SAV >= 33 AND DEC'SAV <= 126 THEN ASC'VAL = CHR$(DEC'SAV) &
                                     ELSE ASC'VAL = "N.A."
TO'RAD:
 IF ASC'VAL = "N.A." THEN DIGIT = "N.A." : GOTO OUT'VAL
 IF LEN(ASC'VAL) = 1 THEN DIGIT = "N.A." : GOTO OUT'VAL
 DIGIT = "" : DECIMAL = DEC'SAV
 DIGIT = MID$(RAD'GUYS,(INT(DECIMAL/1600)+1),1)
 DECIMAL = DECIMAL - (INT(DECIMAL/1600)*1600)
 DIGIT = DIGIT + MID$(RAD'GUYS,(INT(DECIMAL/40)+1),1)
 DECIMAL = DECIMAL - (INT(DECIMAL/40)*40)
 DIGIT = DIGIT + MID$(RAD'GUYS,DECIMAL+1,1)
 GOTO OUT'VAL

RAD'50:
!       This portion of the prg is used only if THREE characters were
!       entered by the user. It calculates the RAD50 equivalent.
!_____________________________________________________________________

       CHAR'STRING = UCS(OP)
       IF OP = ":80" THEN RAD50 = "177777" : GOTO P'RAD
       I = (INSTR(1,RAD'GUYS,MID$(CHAR'STRING,1,1)) - 1)*1600
       J = (INSTR(1,RAD'GUYS,MID$(CHAR'STRING,2,1)) - 1)*40
       K = INSTR(1,RAD'GUYS,MID$(CHAR'STRING,3,1)) - 1
       DECIMAL = I + J + K
       RAD50 = ""
LOOP:
       Y = INT(DECIMAL/8)
       DIGIT = STR$(DECIMAL - (Y * 8))
       RAD50 = DIGIT + RAD50
       DECIMAL = Y
       IF DECIMAL # 0 THEN GOTO LOOP

       RAD50 = RIGHT$(("000000"+RAD50),6)
P'RAD:  PRINT TAB(14,40) RAD50
       GOTO CLEAN'UP

OUT'VAL:
!       Here is where all the values get dumped to the screen
!_____________________________________________________________

 ? TAB(10,40) HEX'VAL TAB(11,40) BIN'VAL TAB(12,40) ASC'VAL
 ? TAB(13,39) DEC'SAV TAB(14,40) DIGIT TAB(15,40) OCT'VAL
 GOTO CLEAN'UP

MOVE'IT:
!       Moves the cursor to the base the user wants to converts
!________________________________________________________________

       CURSOR = CURSOR + 1
       IF CURSOR = 7 THEN CURSOR = 1
       GOSUB DIRECTIONS
       ? TAB(9+CURSOR,40);
       GOTO GET'IT

CLEAN'UP:
!       Simply cleans up the screen and gets ready for the next input
!_____________________________________________________________________

 CNT = ""
 ? TAB(20,20) BLANK2
 ? TAB(21,20) BLANK2
 ? TAB(20,20);: INPUT "Hit [CR] to continue or 'E' to exit ",CNT
 IF CNT <> "" THEN GOTO THE'END
 FOR I = 0 TO 5
       ? TAB(10+I,40) BLANK
 NEXT I
 ? TAB(9+CURSOR,2) BLANK
 ? TAB(20,20) BLANK2
 CURSOR = CURSOR - 1
 GOTO START1

DIRECTIONS:
!       Simply prints the directions during run-time
!______________________________________________________

 ON CURSOR GOTO H,B,A,D,R,O
H: ? TAB(10,2) "Up to 4 digits (0..F)"
  ? TAB(15,2) BLANK : RETURN
B: ? TAB(11,2) "Up to 16 digits (0,1)"
  ? TAB(10,2) BLANK : RETURN
A: ? TAB(12,2) "1 or 3 ASCII letters"
  ? TAB(13,2) "   (blanks = '#')"
  ? TAB(11,2) BLANK : RETURN
D: ? TAB(13,2) BLANK
  ? TAB(12,2) BLANK
  ? TAB(13,2) "Up to 65535" : RETURN
R: ? TAB(14,2) "Up to 6 digits (0..7)"
  ? TAB(13,2) BLANK : RETURN
O: ? TAB(15,2) "Up to 6 digits (0..7)"
  ? TAB(14,2) BLANK : RETURN

THE'END:
PRINT TAB(-1,0)
END