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

Previous | Home | Next