Decimal to Base converter

Highlight the following code and save it as a text file called DECIBASE.BAS
Put it in the same folder as GW-BASIC.EXE. Load it and run it.

10 REM Decimal to Base converter
20 CLEAR :CLS :KEY OFF
30 DIM DIGIT(15)
40 PRINT,"Convert a Decimal to any other base 2 through 16" :PRINT
50 INPUT "Enter a Decimal number less than 32768 ";DECIMAL
60 INPUT "Enter Base 2 through 16 ";BASE
70 IF BASE > 16 OR BASE < 2 OR BASE = 10 THEN 60
80 LET TEMP = DECIMAL
90 N = 0   'count digits in answer
100 WHILE TEMP > 0
110  N = N+1
120  DIGIT(N) = TEMP MOD BASE
130  TEMP = INT(TEMP/BASE)
140 WEND
150 ' Show converted number
160 PRINT :PRINT,DECIMAL; "= ";
170 FOR K = N TO 1 STEP -1
180   IF BASE <= 9 THEN PRINT RIGHT$(STR$(DIGIT(K)),1);
190   IF BASE > 10 THEN IF DIGIT(K) <= 9 THEN PRINT RIGHT$(STR$(DIGIT(K)),1); ELSE GOSUB 240
200 NEXT K
210 PRINT " to base";BASE
220 END
230 '
240 'Base 11 through 16 subroutine
250 IF DIGIT(K) = 10 THEN PRINT "A";
260 IF DIGIT(K) = 11 THEN PRINT "B";
270 IF DIGIT(K) = 12 THEN PRINT "C";
280 IF DIGIT(K) = 13 THEN PRINT "D";
290 IF DIGIT(K) = 14 THEN PRINT "E";
300 IF DIGIT(K) = 15 THEN PRINT "F";
310 RETURN

Previous | Home | Next