Ipquarx

Number Base Converter (Torque)

Jan 13th, 2012
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.39 KB | None | 0 0
  1. //Function: NumToBase
  2. //Usage: NumToBase(Number (Between 0 and 73), Number (Between 2 and 73));
  3. //Purpose: To convert a number from a decimal base to a single Charachter in another base.
  4. //Comments: It took a bit to figure out how to make it without cases.
  5. function numToBase(%a, %b)
  6. {
  7.     if(%a >= %b || %b > 73 || %b < 2) //Exception checking (You can't have base 0 or 1!)
  8.         return -1;
  9.     return getSubStr("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()[];',./<>?", %a, 1);
  10. }
  11.  
  12. //Function: BaseCharToDec
  13. //Usage: BaseCharToDec(Number or Letter (Assumed to be a charachter in another base));
  14. //Purpose: To convert a single base digit to a decimal base.
  15. //Comments: It took a bit to figure out how to make it without cases.
  16. function BaseCharToDec(%a)
  17. {
  18.     %b = "00 11 22 33 44 55 66 77 88 99 A10 B11 C12 D13 E14 F15 G16 H17 I18 J19 K20 L21 M22 N23 O24 P25 Q26 R27 S28 T29 U30 V31 W32 X33 Y34 Z35 a36 b37 c38 d39 e40 f41 g42 h43 i44 j45 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";
  19.     %c = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/@#$%&?<>!";
  20.     %d = strPos(%b, getWord(%b, strPos(%c, %a)));
  21.     return getSubStr(%b, %d + 1, %d > 9 ? 2 : 1);
  22. }
  23.  
  24. //Function: IntDivide
  25. //Usage: IntDivide(Number, Number);
  26. //Purpose: To get the quotient and remainder of a into b.
  27. function intDivide(%a, %b)
  28. {
  29.     return mFloor(%b / %a) SPC %b % %a;
  30. }
  31.  
  32. //Function: DecToBase
  33. //Usage: DecToBase(Decimal Number, Base to be converted to);
  34. //Purpose: To convert a Decimal number into another base.
  35. function DectoBase(%a, %b)
  36. {
  37.     while(%a - strPow(%b, %c) >= 0)
  38.         %c++;
  39.     %c--;
  40.     for(%f = %c; %f >= 0; %f--)
  41.     {
  42.         %d = intDivide(strPow(%b, %f), %a);
  43.         %e = %e @ numToBase(getWord(%d, 0), %b);
  44.         %a = getWord(%d, 1);
  45.     }
  46.     return %e;
  47. }
  48.  
  49. //Function: BaseToDec
  50. //Usage: BaseToDec(Number in any base up to 73, Base of the number entered);
  51. //Purpose: To convert a number in another base to a decimal base.
  52. function BasetoDec(%a, %b)
  53. {
  54.     %d = -1;
  55.     for(%c = strLen(%a) - 1; %c >= 0; %c--)
  56.         %e += strPow(%b, %d++) * BaseCharToDec(getSubStr(%a, %c, 1));
  57.     return %e;
  58. }
  59.  
  60. //Function: BaseToBase
  61. //Usage: BaseToBase(Number in Any Valid Base, Base of number entered, Base to Convert to);
  62. //Purpose: To convert a number in any base to a different base.
  63. function BaseToBase(%a, %b, %c)
  64. {
  65.     return DecToBase(BasetoDec(%a, %b), %c);
  66. }
Advertisement
Add Comment
Please, Sign In to add comment