Decimal to Hex and Hex to Decimal

Developer(s): William Herron
Date: 1981
Type: Program
Platform(s): ZX80

Two short programs by Bill Herron from the Syntax series on number systems. Decimal to Hex asks for a number from -32768 to 32767 and prints it as four hex digits, using AND to pick off each group of four bits; press NEWLINE for another. Hex to Decimal takes up to seven hex digits and prints the decimal value, up to 268435455, carrying in three-digit groups to get around the ZX80’s 16-bit integers.

The article explains how hexadecimal place values work and why hex suits a binary computer. The download has both programs.

Published in Syntax March 1981 (Computer Number Systems II).

Related Articles

Examines hexadecimal system.

Image Gallery

Source Code

   1 REM DECIMAL TO HEX
  10 DIM H(15)
  12 FOR I=0 TO 15
  14 LET H(I)=I+28
  16 NEXT I
 100 PRINT "D=";
 110 INPUT D
 120 PRINT D
 300 PRINT "HX=";
 320 PRINT CHR$(H((D AND -4096)/4096-(D<0)*16));CHR$(H((D AND 3840)/256));CHR$(H((D AND 240)/16));CHR$(H(D AND 15))
1000 INPUT Z$
1050 IF Z$="" THEN GO TO 100

100 REM HX TO DEC
106 DIM X(6)
110 LET X(0)=1
111 LET X(1)=16
112 LET X(2)=256
113 LET X(3)=96
114 LET X(4)=536
115 LET X(5)=576
116 LET X(6)=216
120 DIM Y(6)
123 LET Y(3)=4
124 LET Y(4)=65
125 LET Y(5)=48
126 LET Y(6)=777
130 DIM Z(6)
135 LET Z(5)=1
136 LET Z(6)=16
140 LET A=1000
150 DIM H(6)
200 PRINT "HX=";
220 INPUT H$
230 LET F=0
240 FOR I=0 TO 6
250 LET H=CODE(H$)
251 IF H=1 THEN GO TO 280
252 IF H>28 AND H<44 THEN LET F=1
254 IF H>27 AND H<44 AND F>0 THEN GO TO 260
256 LET H$=TL$(H$)
258 GO TO 250
260 LET H(I)=H-28
265 PRINT CHR$(H);
270 LET H$=TL$(H$)
275 NEXT I
280 LET S=I-1
290 IF S<0 THEN GO TO 293
291 PRINT
292 GO TO 300
293 LET S=0
294 PRINT S
300 LET U=0
302 LET M=0
304 LET L=0
320 FOR I=0 TO S
330 LET X=X(S-I)*H(I)
332 LET Y=Y(S-I)*H(I)
334 LET Z=Z(S-I)*H(I)
340 LET L=L+X
342 LET C=L/A
344 LET L=L-C*A
350 LET M=M+Y+C
352 LET C=M/A
354 LET M=M-C*A
360 LET U=U+Z+C
370 NEXT I
400 PRINT "DEC=";
410 IF U>0 THEN GO TO 414
411 IF M>0 THEN GO TO 416
412 PRINT L
413 GO TO 450
414 PRINT U;
415 GO TO 420
416 PRINT M;
417 GO TO 430
420 PRINT TL$(STR$(M+A));
430 PRINT TL$(STR$(L+A))
450 CLEAR

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