Converting from hexadecimal to decimal number systems has been the bane of many an aspiring machine code programmer. This listing does the converting for you. When you RUN the program, a prompt asks you for a hex number. You can either type one in to get the decimal equivalent, or press ENTER to flip to the decimal to hex mode , where you can type in a base 10 number to get the hex form.
The program can be used as a foundation for a much more elaborate monitor. Subroutines at lines 1000 and 2000 convert the number held in A$ from hex to decimal or decimal to hex respectively. The program returns from the subroutines with the hex number in A$ and the decimal number in C. An error flag (called ERR) is set to 1 if a faulty input is received.
10 LET ERR=0: DEF FN A(H$)=CODE H$-48-7*(H$>"@"): LET H$="0123456789ABCDEF"
20 INPUT "HEX into DEC.?";A$: IF A$="" THEN GO TO 100
30 GO SUB 1000: REM hex to dec
35 IF ERR=1 THEN PRINT "INVALID NUMBER": GO TO 1
40 PRINT A$;" hex=";C;" dec."
50 GO TO 20
100 INPUT "DEC into HEX?";A$: IF A$="" THEN GO TO 10
110 GO SUB 2000: REM dec to hex
120 GO TO 35
1000 LET C=0: FOR X=1 TO LEN A$: IF CODE A$(X)>=48 AND CODE A$(X)<=70 THEN LET C=C+FN A(A$(LEN A$-X+1))*16↑(X-1): NEXT X: RETURN
1010 LET ERR=1: RETURN
2000 FOR X=1 TO LEN A$: IF CODE A$(X)>47 AND CODE A$(X)<58 THEN NEXT X: GO TO 2010
2005 GO TO 1010
2010 LET C=VAL A$: IF C>65535 THEN GO TO 1010
2015 RANDOMIZE C: IF C=0 THEN RETURN
2020 LET P=23671: LET A$=""
2030 LET N=PEEK P: LET N$=H$(N/16+.5): LET N=N-INT (N/16)*16+.5: LET N$=N$+H$(N): LET A$=A$+N$: IF P=23670 THEN RETURN
2040 LET P=P-1: GO TO 2030
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.