Last month we looked at binary numbers, the number system your computer actually thinks in. Now we’ll look at the hexadecimal, or base 16, system.
The hexadecimal system has 16 digits, 0-F. Because our decimal, or base 10, system has only 10 digits, we use letters A-F to represent digits after 9. Thus you count in hex: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. So A₁₆=10₁₀ and F₁₆=15₁₀.
In decimal, each digit represents a power of 10. For example, 209 is equivalent to 2×10² (or 200) plus 0x10¹ (or 0) plus 9×10⁰ (or 9–remember, any number to the zero power equals 1). Similarly, in hex each digit represents a power of 16. So 7FEA equals 7×16³ plus Fx16² plus Ex16¹ plus Ax16⁰, or 32746 in decimal.
So what? The handy thing about hex numbers is that they convert easily with binary, the number system digital computers use, because hex place values are also powers of 2. You can use a single unique hex character to represent any possible combination of 1 and 0 in 4 binary bits. This gives you shorter addresses than in decimal. (Addresses are data location in memory, identified by a number in hex or decimal.) So decoding hex numbers in a binary computer is simple, requiring less memory than to convert decimal numbers to binary.
These 2 programs, written by Bill Herron of San Diego, convert decimal numbers to hex and vice versa. Notice the range of numbers you can use is limited because the ZX80 can only handle numbers that fit in 2 bytes in its arithmetic register. The largest number you can put in 2 bytes is 256×256 or 65536. To use both positive and negative numbers, divide this number by 2 (half each on either side of zero).
DECIMAL TO HEX
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
Syntactic Sum= 20037
Range: -32768≤D≤32767; 0≤HX≤8FFF₁₆
HEX TO DECIMAL
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
Syntactic Sum= 10587
Range: 0₁₆≤HX≤FFFFFFF₁₆; 0≤D≤(2²⁸-1)=268435455
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.