Author Topic: Number Base to Base Converter (Coding Resource)  (Read 2020 times)

Hello, I have made a script that converts numbers to and from different bases.
It currently works on any base from 2 to 73.
The bases go from 0-9, A-Z, a-z, +, /, @, #, $, %, &, ?, <, >, !.
It is case-sensitive.

The Code:
Code: [Select]
//Function: NumToBase
//Usage: NumToBase(Number (Between 0 and 73), Number (Between 2 and 73));
//Purpose: To convert a number from a decimal base to a single Charachter in another base.
//Comments: It took a bit to figure out how to make it without cases.
function numToBase(%a, %b)
{
if(%a >= %b || %b > 73 || %b < 2) //Exception checking (You can't have base 0 or 1!)
return -1;
return getSubStr("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()[];',./<>?", %a, 1);
}

//Function: BaseCharToDec
//Usage: BaseCharToDec(Number or Letter (Assumed to be a charachter in another base));
//Purpose: To convert a single base digit to a decimal base.
//Comments: It took a bit to figure out how to make it without cases.
function BaseCharToDec(%a)
{
%b = "00 11 22 33 44 55 66 77 88 99 A10 B11 C12 D13 E14 F15 G16 H17 I18 J19 K20 L21 22 N23 O24 P25 Q26 R27 S28 T29 U30 V31 W32 X33 Y34 Z35 a36 b37 c38 d39 e40 f41 g42 h43 i44 45 k46 l47 m48 n49 o50 p51 q52 r53 s54 t55 u56 v57 w58 x59 y60 z61 +62 /63 @64 #65 $66 67 &68 ?69 <70 >71 !72";
%c = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/@#$%&?<>!";
%d = strPos(%b, getWord(%b, strPos(%c, %a)));
return getSubStr(%b, %d + 1, %d > 9 ? 2 : 1);
}

//Function: IntDivide
//Usage: IntDivide(Number, Number);
//Purpose: To get the quotient and remainder of a into b.
function intDivide(%a, %b)
{
return mFloor(%b / %a) SPC %b % %a;
}

//Function: DecToBase
//Usage: DecToBase(Decimal Number, Base to be converted to);
//Purpose: To convert a Decimal number into another base.
function DectoBase(%a, %b)
{
while(%a - mPow(%b, %c) >= 0)
%c++;
%c--;
for(%f = %c; %f >= 0; %f--)
{
%d = intDivide(mPow(%b, %f), %a);
%e = %e @ numToBase(getWord(%d, 0), %b);
%a = getWord(%d, 1);
}
return %e;
}

//Function: BaseToDec
//Usage: BaseToDec(Number in any base up to 73, Base of the number entered);
//Purpose: To convert a number in another base to a decimal base.
function BasetoDec(%a, %b)
{
%d = -1;
for(%c = strLen(%a) - 1; %c >= 0; %c--)
%e += mPow(%b, %d++) * BaseCharToDec(getSubStr(%a, %c, 1));
return %e;
}

//Function: BaseToBase
//Usage: BaseToBase(Number in Any Valid Base, Base of number entered, Base to Convert to);
//Purpose: To convert a number in any base to a different base.
function BaseToBase(%a, %b, %c)
{
return DecToBase(BasetoDec(%a, %b), %c);
}
Please note that improper use of this can lead to crashing.
It successfully converts between bases of 2-73 with numbers below 1 million for operations involving decimal without problems.
Sometimes it won't convert perfectly due to the lack of higher-precision math in Blockland.

The code length in total (without comments) is 46 lines.

"coding help"

so what do you need help with


Also lol I remember telling you to add base conversion to your chat math mod, and you told me it was useless.

so what do you need help with
It's a resource for other people that may want help.

Also lol I remember telling you to add base conversion to your chat math mod, and you told me it was useless.
For math, it's useless for math.

converting beyond base 36 is pointless