Number Base Converter

Author(s): Syd Wyncoop
Pub Details: Time Designs Magazine v3 n4 (Volume 3, Issue 4)
Date: May/June 1987
Pages: 31

This program runs on the TS2068 or the ZX81

This is a useful BASIC routine to do conversions between number bases. It will run on both the ZX81 and TS2068. I have included in the listing the necessary changes for the ZX81 (or TS1000/TS1500). The only other requirement to run on the ZX81 would be to break the multi-statement lines into all single statement lines.

   1 BORDER 0: PAPER 0: INK 4: CLS
2 ON ERR GO TO 800
3 GO TO 1000
100 REM Decimal to Hex
Entry:a=decimal number
Exit :a=decimal number
h$=hex number
110 LET h$="": LET hh=INT (a/256): LET hl=a-hh*256
120 IF hh<1 THEN LET h$="00": GO TO 140
130 LET n=hh: GO SUB 150
140 LET n=hl
150 LET n1=INT (n/16): LET n2=n-n1*16
160 LET h$=h$+(CHR$ (n1+48+(7 AND n1>9)))+(CHR$ (n2+48+(7 AND n2>9)))
170 RETURN
180 REM TS1000 users replace line 160 with: LET h$=h$+CHR$ (n1+28)+CHR$ (n2+28)
200 REM Hex to Decimal
Entry:a$=hex number
Exit :a$=hex number
a=decimal number
210 LET h$=a$: LET a=0
220 IF LEN h$<3 THEN GO TO 250
230 GO SUB 250
240 LET a=a*256: LET h$=h$(3 TO )
250 LET a=a+(CODE h$-48-(7 AND h$>"9"))*16+(CODE h$(2)-48-(7 AND h$(2)>"9"))
260 RETURN
280 REM TS1000 users replace line 250 with: LET a=a+(CODE h$-28)*16+(CODE h$(2)-28)
300 REM Decimal to Binary
Entry:a=decimal number
Exit :a=decimal number
b$=binary number
310 LET b$="00000000": LET n=a: IF n>255 THEN LET n=INT (n/256): LET n1=a-n*256: GO SUB 360: LET a$=b$: LET B$="00000000": LET n=n1: GO SUB 360: LET b$=a$+b$: RETURN
360 FOR i=7 TO 0 STEP -1
370 IF n>=2^i THEN LET b$(8-i)="1": LET n=n-2^i
380 NEXT i: RETURN
400 REM Binary to Decimal
Entry:a$=binary number
Exit :a$=binary number
a=decimal number
410 LET b$=a$: LET a=0: LET n=LEN b$-1
420 FOR i=n TO 0 STEP -1
430 IF b$(1)="1" THEN LET a=a+2^i
440 LET b$=b$(2 TO ): NEXT i
450 RETURN
500 REM Set Decimal Tabs
510 LET t=4
520 LET T=T-(1 AND A>10000)-(1 AND A>1000)-(1 AND A>100)-(1 AND A>10)
530 RETURN
800 REM Error Trap
810 PRINT #0;"Invalid Entry--Please Try Again": PAUSE 150
820 GO TO 1030
1000 REM Main Loop
1010 PRINT ''TAB 8;"NUMBER CONVERTER"'''
1020 PRINT "Be sure to append a 'B' or 'H'"'"to all binary and hexidecimal"'"entries, respectively."''"There is no suffix required for decimal numbers, as they are thedefault."
1030 POKE 23658,8: INPUT AT 0,0;"Type number and press ENTER"' LINE a$: LET n=LEN a$
1040 IF a$(n)="D" THEN LET a$=a$( TO n-1): GO TO 1070
1050 IF a$(n)="H" THEN LET a$=a$( TO n-1): GO SUB 200: GO TO 1080
1060 IF a$(n)="B" THEN LET a$=a$( TO n-1): GO SUB 400: GO TO 1080
1070 LET A=VAL A$
1080 GO SUB 100: GO SUB 300: GO SUB 500
1090 PRINT AT 16,0;" Dec Hex Bin"'"-------------------------------- "
1100 PRINT AT 18,T;a;TAB 8;h$;"h";TAB 15;b$;"b"
1110 GO TO 1030
9990 MOVE "NumConv.bas",1

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.