'=========================================================================== ' Subject: UPDATE OF BASE CONVERSION Date: 11-14-96 (16:16) ' Author: Tyler Barnes Code: QB, QBasic, PDS ' Origin: comp.lang.basic.misc Packet: ALGOR.ABC '=========================================================================== 'Here's an update to my base conversion thing. This version is smaller, 'faster, and neater. (I simplified a part at the end and removed some of 'the exponent stuff I was doing.) DECLARE SUB Base2Base (Number1$, Digits1$, Number2$, Digits2$) CONST Bin = "01", Oct = "01234567", Dec = "0123456789", Hex = "0123456789ABCDEF SUB Base2Base (Number1$, Digits1$, Number2$, Digits2$) Number1$ = UCASE$(Number1$): Digits1$ = UCASE$(Digits1$) Digits2$ = UCASE$(Digits2$): Number2$ = "" IF Digits1$ <> "0123456789" THEN FOR I% = LEN(Number1$) TO 1 STEP -1 IF Digits1$ = "01234567" THEN FinalNum& = VAL("&O" + Number1$): EXIT FOR IF Digits1$="0123456789ABCDEF" THEN FinalNum&=VAL("&H"+Number1$):EXIT FOR CD$ = MID$(Number1$, I%, 1) CV% = INSTR(Digits1$, CD$) - 1 FinalNum& = FinalNum& + (CV% * (LEN(Digits1$) ^ ABS(I% - LEN(Number1$)))) NEXT I% ELSE FinalNum& = VAL(Number1$) END IF IF FinalNum& < 0 THEN FinalNum& = FinalNum& + 65536 IF Digits2$ = "0123456789" THEN Number2$ = LTRIM$(STR$(FinalNum&)): EXIT SUB IF Digits2$ = "0123456789ABCDEF" THEN Number2$ = HEX$(FinalNum&): EXIT SUB IF Digits2$ = "01234567" THEN Number2$ = OCT$(FinalNum&): EXIT SUB DestBase% = LEN(Digits2$) DO Remainder% = FinalNum& MOD DestBase% FinalNum& = (FinalNum& - Remainder%) / DestBase% Number2$ = MID$(Digits2$, Remainder% + 1, 1) + Number2$ LOOP UNTIL FinalNum& = 0 END SUB