1/1/1982 Toward Easy ConversionI have been reading Softalk for the past few years now and I must say that it has come a long way in that time. I have found that among the most helpful articles in Softalk are Basic Solution and Assembly Lines. One difficulty some of us have is converting hex numbers to decimal and decimal to hex. To that end, I submit a program, which I think will help my fellow Apple users. Here is how it works:
10 REM **THIS PROGRAM IS DESIGNED TO CONVERT HEX ADDRESSES TO DECIMAL OR DECIMAL TO HEX . 20 DIM A%(4,1),D$(4,1) 30 CLEAR 40 PRINT "ENTER INPUT NUMBER FORMAT 'H' = HEX, 'D' = DECIMAL?": GET 0$ 50 IF 0$ = "D" THEN GOTO 220 60 IF ASC (0$) = 13 THEN PRINT "BYE": END 70 IF 0$ < > "H" THEN PRINT "INVALID OPTION ENTER 'H' OR "D"': GOTO 40 80 PRINT : INPUT "ENTER HEX NUMBER TO BE CONVERTED? "; A$ 90 L = LEN (A$): IF L = 0 OR L > 4 THEN PRINT "INVALID ENTRY, REENTER": GOTO 30 100 FOR J = 1 TO L:N = L - (J - 1) 110 FOR I = 0 TO 15 120 READ B$ 130 IF B$ = MID$ (A$,N,1) THEN A%(J,1) = I: GOTO 170 140 NEXT I 150 PRINT "AN ILLEGAL CHARACTER WAS DETECTED IN THE INPUT STRING. IT IS -" MID$ (A$,J,1): RESTORE : GOTO 30 160 GOTO 30 170 RESTORE : NEXT J 180 D = A%(1,1) -I- (16 (A%(2,1) ) ) -I- (256 *(A%(3,1) ) ) + ( (16*16*16) * (A%(4,1) )) 190 PRINT "THE DECIMAL EQUIVALENT OF'"AS" ' IS "D200 PRINT 210 GOTO 30 220 PRINT : INPUT "ENTER DEC NUMBER TO BE CONVERTED? ";R 230 IF R = 0 OR R > 65535 THEN PRINT "INVALID DECIMAL ENTRYI": GOTO 220 240 C = R 250 A%(1,1) = R / 4096 260 R = R - (4096 * (A%(1,1) ) ) 270 A%(2,1) = R / 256280 R = R - (256 * (A%(2,1) ) ) 290 A%(3,1) = R / 16 300 A%(4,1) = R - (16 * (A%(3,1) ) ) 310 FOR J = 1 TO 4 320 FOR I = 0 TO 15 330 READ B$ 340 IF I = A%(J,1) THEN D$(J,1) = Bl GOTO 370 350 NEXT I 360 PRINT "DECIMAL NUMBER OUT OF RANGE FOR 4 HEX BYTES": GOTO 30 370 RESTORE : NEXT J 380 E$ = D$(1,l) -I- D$(2,1) + D$(3,1) -I- D$(4,1) 390 PRINT : PRINT "THEN HEX EQUIVALENT OF' "C" ' IS "E$ 400 PRINT 410 GOTO 30 420 DATA 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F430 REM ***** THIS PROGRAM DONATED BY RICHARD A. PAHON, 362 HASTINGS BLVD., BROOMALL, PA 19008 Lines 40 to 70 are used to select and validate the option "H" or "D." Line 80 requests the hex address as a string variable. Line 90 validates the input. Line 100 establishes the for-next loop used to check each input character, the N variable is created to invert the J sequence since the mid$ step in line 130 reads characters from left to right (left justification for characters). The for-next loop of lines 110 to 140 is used to check each input character against the data statement in line 420. Then use the value of I (which corresponds to the match of "B$" to input character J) to load the array A%, J. Line 150 detects an illegal character. Line 170 resets the data pointer to entry #1.Line 180 is the heart of the hex-dec conversion; it raises each variable in the array A% to the appropriate power of sixteen and then sums the results into variable D. Line 190 prints the results. Line 220 is used when dec to hex conversion is requested. The input is stored as a real number since Apple will not accept an integer number larger than 32767 and $FFFF is 65535 in decimal. Line 230 validates the input. Line 240 saves the input in variable C for later display. Lines 250 to 300 convert the input "R" into an equivalent integer variable A%. Each A% variable is loaded with the appropriate number of times that the power of 16 (4096, 256, 16, 1) will divide into R, the input. Each time a division occurs, the product of sixteen raised to the appropriate power and the integer value A% is subtracted from the input variable R. Thus the remainder is paussed on to the next lower power of sixteen for division. Line 310 starts the for-next loop that will load the output variables. Lines 320 and 350 match the integer loaded in A%,J against I; when a match is found the corresponding B$ variable represents the hex equivalent of the integer A%,J. The appropriate B$ variable is then loaded into the string array D$, J. Line 360 is printed when no hex match can be found. Line 370 restores the data pointer as above. Line 380 is used to concatenate all of the D$ variables into the output variable E$. Line 390 prints the result of the conversion. Line 420 is the data statement that represents all the possible hex designations. I hope you will accept this as my thanks for the good work you folks are doing. Richard A. Patton, Broomall, PA - V2N5 Comments are closed.
|