D-B-H Conversion

Author(s): Kenneth Sell
Publication: Syntax
Pub Details: Syntax/Syntax ZX80 v4 n10 (Volume 4, Issue 10)
Date: October 1983
Pages: 4-5

Convert decimal, binary and hexadecimal numbers easily with this well prompted utility.

Type in the program and RUN or GOTO 500. Press D, B or H as prompted, depending on the number you wish to convert. Enter numbers imitating the style as prompted. For example, you must enter binary numbers with 16 digits in groups of four. Your screen looks like this after you ENTER a number.

 A15         8 7         0
B= 0100 1111 1110 1000
H=    4    F    E    8
D= 256 *79 +   232    =20456

ENTER D,H OR B FOR NEXT NO.
ENTER "S" TO STOP, "C" TO COPY

Four labeled lines appear; A defines the address bits of a 16-bit bus. B expresses the binary value, grouped in 4-bit clusters. H gives the translation of each cluster to its hex equivalent. D shows the translation to decimal, by byte (two clusters) and the decimal sum. When you press S to stop, you see report code 9/line #. SAVE with GOTO 900; the program then auto-RUNs when you LOAD. RUN 500 to start again. Press C to copy the screen display; this will not interrupt program execution.

  15 LET B=32768
20 LET A=VAL C$
25 FOR I=1 TO 19
30 IF I=5 OR I=10 OR I=15 THEN NEXT I
35 LET C=INT (A/B)
40 LET D$(1,I)=STR$ C
45 IF A>=B THEN LET A=A-B
50 LET B=B/2
55 NEXT I
60 RETURN
210 LET B=4096
215 LET C=VAL C$
220 FOR J=1 TO 4
225 LET A=INT (C/B)
230 IF A<10 THEN LET B$(1,J)=STR$ A
235 IF A>9 THEN LET B$(1,J)=CHR$ (A+28)
240 LET C=C-A*B
245 LET B=B/16
250 NEXT J
255 RETURN
300 LET A=10
305 LET B=11
310 LET C=12
315 LET D=13
320 LET E=14
325 LET F=15
330 LET AA=VAL B$(1,4)*1+VAL B$(1,3)*16+VAL B$(1,2)*256+VAL B$(1,1)*4096
335 LET C$=STR$ AA
340 RETURN
400 LET B=0
405 LET A=1
410 FOR I=19 TO 1 STEP -1
415 IF I=15 OR I=10 OR I=5 THEN NEXT I
420 LET B=B+A*VAL D$(1,I)
425 LET A=A*2
430 NEXT I
435 LET C$=STR$ B
440 RETURN
500 DIM D$(1,19)
501 DIM B$(1,4)
505 PRINT AT 10,4;"ENTER H FOR HEXADECIMAL"
510 PRINT AT 11,10;"D FOR DECIMAL"
515 PRINT AT 12,10;"B FOR BINARY"
520 INPUT A$
525 IF A$="H" THEN GOTO 545
530 IF A$="D" THEN GOTO 580
535 IF A$="B" THEN GOTO 610
540 GOTO 520
545 CLS
550 PRINT AT 20,0;"ENTER HEX NO. ""0000"" "
555 INPUT Q$
556 IF LEN Q$<>4 THEN GOTO 555
560 LET B$(1)=Q$
561 FOR I=1 TO 4
562 IF CODE B$(1,I)>43 OR CODE B$(1,I)<28 THEN GOTO 555
563 NEXT I
564 FAST
565 GOSUB 300
570 GOSUB 15
575 GOTO 700
580 CLS
585 PRINT AT 20,0;"ENTER DECIMAL NO. ""0 TO 65535"" "
590 INPUT C$
591 IF VAL C$>65535 THEN GOTO 585
592 FAST
595 GOSUB 210
600 GOSUB 15
605 GOTO 700
610 CLS
615 PRINT AT 20,0;"ENTER BINARY NO.""1 OR 0"" ""0000 0000 0000 0000"" "
620 INPUT Z$
621 IF LEN Z$<>19 THEN GOTO 615
622 LET D$(1)=Z$
623 FAST
625 GOSUB 400
630 GOSUB 210
700 SLOW
705 PRINT AT 20,0;"A15 8 7 0 B= ";D$(1);" "
710 SCROLL
715 SCROLL
720 PRINT AT 20,0;"H= ";B$(1,1);" ";B$(1,2);" ";B$(1,3);" ";B$(1,4)
725 SCROLL
730 PRINT AT 20,0;"D= 256 *";INT (VAL C$/256);AT 20,12;"+ ";VAL C$-256*INT (VAL C$/256);AT 20,22;"=";VAL C$
735 SCROLL
740 SCROLL
745 PRINT AT 20,0;"ENTER D,H OR B FOR NEXT NO. ENTER ""S"" TO STOP, ""C"" TO COPY"
750 INPUT A$
755 IF A$="D" THEN GOTO 585
760 IF A$="H" THEN GOTO 550
765 IF A$="B" THEN GOTO 615
770 IF A$="C" THEN COPY
775 IF A$="S" THEN STOP
780 GOTO 745
900 SAVE "D TO B TO H"
901 GOTO 500
SYNTACTIC SUM: 60642, 8K ROM

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