(03-24-2021 03:44 PM)SlideRule Wrote: An excerpt from Science and Engineering Sourcebook, pgs 23-24:
Any Base to Any Base Conversion
Description
Converting numbers between different bases is frequently done by computer programmers, amthematicians, high school and college students. In particular, computer program development requires conversion between binary (2), octal (8), decimal (10) and hexadecimal (16) bases. Number tjeory problems may require conversion between these and other bases.
Conversion between two arbitrary bases a and b is generally a two step procedure, converting first from base a to base 10 and then from base 10 to base b. The unique method presented here for positive integers uses the same algorithm for both conversions, thus saving on programming steps.
To represent a number to base larger than 10, more than one place is necessary to represent one digit in that base. For example 17FE (base 16) would be entered or displayed as <01>, <07>, <15>, <14> or simply 1071514 (1=01, 2=02, …, A=10, B=11, C=12, D=13, E=14, F=15). In general, one place would be reserved per digit for bases 2-10, two places for bases 11-100, etc. The program automatically interprets the entry or display depending on the base. The program has no inherent limitations on the magnitude of the base or the number to be converted. The only limiting factor is the computer's accuracy. For example the 10 digit accuracy of the Pocket Computer limits the input and output to 10 decimal places. A larger number would lead to error.
PROGRAM LISTING
90: "Z"CLEAR : PAUSE "BASE CONVERSION"
100: INPUT "ARGUMENT?";C: F=C: G=110: GOTO 290
110: INPUT "OLD BASE?";D: F=D: G=120: GOTO 290
120: INPUT "NEW BASE?";E: F=E: G=130: GOTO 290
130: IF D<>10 GOTO 150
140: N=C: GOTO 170
150: U=D: GOSUB 280
160: S=C: Q=D: R=V: GOSUB 240
170: IF E<>10 GOTO 190
180: S=N: GOTO 210
190: U=E: GOSUB 280
200: S=N: Q=V: R=E: GOSUB 240
210: BEEP 1: USING "###########"
220: PAUSE C;" BASE";USING "####";D:
PRINT USING "###########";"=";N;" BASE";
USING "####;E
230: PRINT USING "###########";"=";S;" BASE 10":
GOTO 100
240: M=0: N=0: P=S
250: T=P: P=INT (P/R): N=N+INT ((T-P*R)*Q^M+.5)
260: IF P=0 RETURN
270: M=M+1: GOTO 250
280: V=10^(1+INT LOG (U-1)): RETURN
290: IF F=INT ABS F GOTO G
300: G=G-10: BEEP 2: PAUSE "BAD INPUT": GOTO G
INSTRUCTION
Press Shift Z to start the program. Follow the prompts by entering the number to be converted (argument) the old and new base. You can optionally display the argument to base 10 after seeing the argument to the new base by pressing Enter after display of the result. For proper display, change the output format by adding additional "#" if one of the bases is larger than 999.
EXAMPLE
Convert 17C (base 16) to base 2.
KEY-IN DISPLAY REMARKS
Shift Z BASE CONVERSION
ARGUMENT? 17C(hex)=10712
10712 Ent OLD BASE?
16 Ent NEW BASE? Beeps when ready
2 Ent 10712 BASE 16
=101111100 BASE 2
Ent =380 BASE 10 Optional step
Ent Prompt for a new case
BEST!
SlideRule
On some machines you can convert from base 2, 8 or 16 to base 10 by doing this:
VAL("&B"+"11011011")
VAL("&O"+"7413")
VAL("&H"+"CF1A")
and back
PRINT BIN$(74)
PRINT OCT$(912)
PRINT HEX$(65521)