'=========================================================================== ' Subject: CONVERT DEC TO ANY OTHER BASE Date: 11-20-99 (22:22) ' Author: James Salim Code: QB, QBasic, PDS ' Origin: jsalim@dis.net.au Packet: BINARY.ABC '=========================================================================== DECLARE SUB Demo () DECLARE FUNCTION Base2Dec# (InputVal$, nBase%) DECLARE FUNCTION Dec2Base$ (InputVal AS DOUBLE, nBase%) DECLARE SUB Initialize () CONST MaxBase% = 35 DIM SHARED ValueSub$(MaxBase%) '=========================================================================== 'This program consist of 2 function that can be used to convert decimal to 'any number base (2 to ..) and vice versa. 'This program is different from any other in that it not only allow you to convert 'to base 2(binary), 8(Octal) and 16(Hexadecimal), but it also let you to convert 'to any bases eg.2,3,4,5,6, etc. 'At this current point, I only have set its maximum base to 35, because I can't 'think of any character for number 36, we could use symbols, but I left it for 'the moment. ' 'Thats all for now, any comments, mail me at jsalim@mail.com, or ICQ #18495651 ' 'Thank you, 'James Salim '=========================================================================== Initialize Demo END ErrorProc: SELECT CASE ERR CASE 255: LOCATE 22, 1: PRINT "Error: Number base out of range" CASE 254: LOCATE 22, 1: PRINT "Error: Invalid Value Input" CASE 6: LOCATE 22, 1: PRINT "Error: Over flow" CASE ELSE: LOCATE 22, 1: PRINT "Error: Error Number "; ERR END SELECT RESUME NEXT FUNCTION Base2Dec# (InputVal$, nBase%) DIM TempArray(1 TO 32767) AS INTEGER IF nBase% < 2 OR nBase% > MaxBase% THEN ERROR 255 'Base out of range EXIT FUNCTION END IF FOR i = 1 TO LEN(InputVal$) Correct% = 0 FOR j = 0 TO nBase% - 1 IF MID$(InputVal$, i, 1) = ValueSub$(j) THEN TempArray(i) = j Correct% = 1 EXIT FOR END IF NEXT j IF Correct% = 0 THEN ERROR 254 'Invalid Input EXIT FUNCTION END IF NEXT i TempOut# = 0 MaxPos = LEN(InputVal$) FOR i = 1 TO LEN(InputVal$) TempOut# = TempOut# + TempArray(i) * (nBase% ^ (MaxPos - i)) NEXT i Base2Dec# = TempOut# END FUNCTION FUNCTION Dec2Base$ (InputVal AS DOUBLE, nBase%) IF InputVal < 0 OR InputVal > 999999999999999# THEN ERROR 254 'Invalid Input EXIT FUNCTION END IF IF nBase% < 2 OR nBase% > MaxBase% THEN ERROR 255 'Base out of range EXIT FUNCTION END IF DIM TempValue AS DOUBLE TempValue = InputVal IF InputVal < 0 THEN TempValue = InputVal * -1 TempOutput$ = "" DO WHILE TempValue >= nBase% TempOutput$ = TempOutput$ + ValueSub$(TempValue - INT(TempValue / nBase%) * nBase%) TempValue = INT(TempValue / nBase%) LOOP TempOutput$ = TempOutput$ + ValueSub$(TempValue - INT(TempValue / nBase%) * nBase%) 'TempOutput$ = TempOutput$ + ValueSub$(TempValue MOD nBase%) TempOutput2$ = "" FOR i = LEN(TempOutput$) TO 1 STEP -1 TempOutput2$ = TempOutput2$ + MID$(TempOutput$, i, 1) NEXT i Dec2Base$ = TempOutput2$ END FUNCTION SUB Demo ON ERROR GOTO ErrorProc '============================================================================ 'FIRST DEMO '============================================================================ CLS LOCATE 1, 1: PRINT "First Demo : Convert decimal value to 35 number bases" PRINT INPUT "Enter a positive decimal value (0-999,999,999,999,999): ", InputVal# CLS PRINT "Decimal Value -> InputVal#" LOCATE 3, 1: PRINT "Base 2 (Binary) - "; Dec2Base$(InputVal#, CINT(2)) FOR i = 3 TO 7 LOCATE i, 1: PRINT "Base "; LTRIM$(STR$(i)); " - "; Dec2Base$(InputVal#, CINT(i)) NEXT i LOCATE 8, 1: PRINT "Base 8 (Octal) - "; Dec2Base$(InputVal#, CINT(8)) LOCATE 9, 1: PRINT "Base 9 - "; Dec2Base$(InputVal#, CINT(9)) LOCATE 10, 1: PRINT "Base 10 (Decimal) - "; Dec2Base$(InputVal#, CINT(10)) FOR i = 11 TO 20 LOCATE i, 1: PRINT "Base "; LTRIM$(STR$(i)); " - "; Dec2Base$(InputVal#, CINT(i)) NEXT i LOCATE 22, 1: PRINT "Press any key to continue..." a$ = INPUT$(1) CLS PRINT "Decimal Value -> InputVal#" FOR i = 21 TO 35 LOCATE i - 19, 1: PRINT "Base "; LTRIM$(STR$(i)); " - "; Dec2Base$(InputVal#, CINT(i)) NEXT i LOCATE 22, 1: PRINT "Press any key to continue..." a$ = INPUT$(1) '============================================================================ 'SECOND DEMO '============================================================================ CLS LOCATE 1, 1: PRINT "Second Demo : Convert number from other bases to decimals" PRINT INPUT "Enter the base number you want to convert from (eg. 2 for binary): ", nBase% PRINT INPUT "Enter the value for the number base (Any alphanumeric) : ", InputVal$ PRINT CLS PRINT "Input Value : "; UCASE$(InputVal$); " (Base "; LTRIM$(STR$(nBase%)); ")" PRINT PRINT "Output Value (Decimal) : "; Base2Dec#(InputVal$, nBase%) LOCATE 22, 1: PRINT "Press any key to continue..." a$ = INPUT$(1) '============================================================================ 'THIRD DEMO '============================================================================ CLS LOCATE 1, 1: PRINT "Third Demo : Convert number from any bases to different number base" PRINT INPUT "Enter the base number you want to convert from (eg. 2 for binary): ", nBase% PRINT INPUT "Enter the value for the first number base (Any alphanumeric) : ", InputVal$ PRINT INPUT "Enter the base number you want to convert to (eg. 2 for binary): ", OutnBase% CLS PRINT "Input Value : "; UCASE$(InputVal$); " (Base "; LTRIM$(STR$(nBase%)); ")" PRINT PRINT "Output Value : "; Dec2Base$(Base2Dec#(InputVal$, nBase%), OutnBase%); " (Base "; LTRIM$(STR$(OutnBase%)); ")" LOCATE 22, 1: PRINT "Press any key to continue..." a$ = INPUT$(1) '============================================================================ 'ENDINGS '============================================================================ CLS PRINT "That concludes the demo" PRINT "I will now explains the functions" PRINT PRINT "1. Dec2Base$ (InputVal#, nBase%)" PRINT " * Purpose: Convert decimal to number base [nBase%]" PRINT " * Input - InputVal# -- Any positive decimal (0-999,999,999,999,999)" PRINT " - nBase% -- Output number base" PRINT PRINT "2. Base2Dec# (InputVal$, nBase%)" PRINT " * Purpose: Convert number base [nBase%] to decimal" PRINT " * Input - InputVal$ -- Any value based on the number base " PRINT " - nBase% -- Input number base" PRINT PRINT "Thanks for taking your time to try this functions, I would gladly accept your comments, whatever. I could be contacted both by e-mail or ICQ. Here is my detail" PRINT "James Salim" PRINT "e-mail: jsalim@mail.com" PRINT "ICQ: 18495651" EXIT SUB END SUB SUB Initialize FOR i = 0 TO 9 ValueSub$(i) = LTRIM$(STR$(i)) NEXT i FOR i = 1 TO 26 ValueSub$(i + 9) = CHR$(i + 64) NEXT i END SUB