--- title: "Decimal to Hex and Hex to Decimal" id: 76396 type: "computer_media" slug: "decimal-to-hex-and-hex-to-decimal" url: "http://localhost/computer_media/decimal-to-hex-and-hex-to-decimal/" markdown_url: "http://localhost/computer_media/decimal-to-hex-and-hex-to-decimal.md" published_at: "2026-11-11T08:02:48+00:00" modified_at: "2026-11-11T08:02:48+00:00" author: "David Anderson" featured_image: url: "http://localhost/wp-content/uploads/2026/11/decimal-hex-converters-1.png" excerpt: "Two ZX80 converters from the Computer Number Systems series: decimal numbers to four-digit hex, and hex numbers of up to seven digits back to decimal." post_tag: - name: "Downloadable" slug: "downloadable" taxonomy: "post_tag" url: "http://localhost/tag/downloadable/" - name: "ZX80" slug: "zx80" taxonomy: "post_tag" url: "http://localhost/tag/zx80/" model: - name: "Sinclair ZX80" slug: "zx80" taxonomy: "model" url: "http://localhost/model/zx80/" indiv: - name: "William Herron" slug: "herron-william" taxonomy: "indiv" url: "http://localhost/indiv/herron-william/" genre: - name: "Mathematics" slug: "mathematics" taxonomy: "genre" url: "http://localhost/type/mathematics/" media_type: "Program" programmers: - name: "William Herron" slug: "herron-william" taxonomy: "indiv" url: "http://localhost/indiv/herron-william/" download_url: "https://archive.org/download/timex-sinclair-software-archive/Decimal%20to%20Hex%20and%20Hex%20to%20Decimal%20%281981%29%28Herron%2C%20William%29%28ZX80%29%28US%29%28Program%29.zip" mediadate: "1981" images: - url: "http://localhost/wp-content/uploads/2026/11/decimal-hex-converters-1.png" - url: "http://localhost/wp-content/uploads/2026/11/decimal-hex-converters-2.png" article_media: - id: 19783 title: "Computer Number Systems II" type: "article" url: "http://localhost/article/computer-number-systems-ii/" media_type_tags: "Mathematics" license: false --- # Decimal to Hex and Hex to Decimal 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](https://timexsinclair.com/article/computer-number-systems-ii/)). ## 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 ```