Advertisement
EmilySamantha80

BaseX math (Math for numbers with any character set)

Dec 15th, 2016
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.37 KB | None | 0 0
  1. // Title:  BaseX math (Math for numbers with any character set)
  2. // Author: Emily Heiner
  3. //
  4. // MIT License
  5. // Copyright(c) 2017 Emily Heiner (emilysamantha80@gmail.com)
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in all
  15. // copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. // SOFTWARE.
  24.  
  25. using System;
  26. using System.Collections.Generic;
  27.  
  28. namespace ESH.Utility
  29. {
  30.     public class BaseX
  31.     {
  32.         // Built-in BaseX alphabets
  33.         public const string Base2Alphabet = "01";
  34.         public const string Base10Alphabet = "0123456789";
  35.         public const string Base16Alphabet = "0123456789ABCDEF";
  36.         public const string Base26Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  37.         public const string Base36Alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  38.         public const string Base62Alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  39.  
  40.         /// <summary>
  41.         /// Converts an Int64 into a BaseX number
  42.         /// </summary>
  43.         /// <param name="input">Int64 number to convert</param>
  44.         /// <param name="baseAlphabet">BaseX alphabet to use</param>
  45.         /// <returns>BaseX result</returns>
  46.         public static string FromInt64(Int64 input, string baseAlphabet)
  47.         {
  48.             char[] baseChars = baseAlphabet.ToCharArray();
  49.             var result = new Stack<char>();
  50.             bool isNegative = false;
  51.             if (input < 0)
  52.             {
  53.                 isNegative = true;
  54.                 input = input * -1;
  55.             }
  56.  
  57.             if (input == 0)
  58.             {
  59.                 return baseAlphabet[0].ToString();
  60.             }
  61.  
  62.             while (input != 0)
  63.             {
  64.                 result.Push(baseChars[input % baseAlphabet.Length]);
  65.                 input /= baseAlphabet.Length;
  66.             }
  67.  
  68.             string resultString = new string(result.ToArray());
  69.             if (isNegative)
  70.             {
  71.                 resultString = "-" + resultString;
  72.             }
  73.             return resultString;
  74.         }
  75.  
  76.         /// <summary>
  77.         /// Converts a BaseX number into an Int64
  78.         /// </summary>
  79.         /// <param name="input">BaseX number to convert</param>
  80.         /// <param name="baseAlphabet">BaseX alphabet to use</param>
  81.         /// <returns>Int64 result</returns>
  82.         public static Int64 ToInt64(string input, string baseAlphabet)
  83.         {
  84.             if (input == null)
  85.             {
  86.                 throw new ArgumentNullException("input", "Argument must not be null.");
  87.             }
  88.  
  89.             Int64 result = 0;
  90.             for (int i = 0; i < input.Length; i++)
  91.             {
  92.                 if (input[i] != '-')
  93.                 {
  94.                     char c = input[i];
  95.                     if (baseAlphabet.IndexOf(c) == -1)
  96.                     {
  97.                         throw new ArgumentOutOfRangeException("input", "Argument contains invalid characters.");
  98.                     }
  99.                     result = result + (Int64)(baseAlphabet.IndexOf(c) * Math.Pow(baseAlphabet.Length, input.Length - i - 1));
  100.                 }
  101.             }
  102.  
  103.             if (input[0] == '-')
  104.             {
  105.                 result = result * -1;
  106.             }
  107.  
  108.             return result;
  109.         }
  110.  
  111.         /// <summary>
  112.         /// Adds a specified integer to a BaseX number
  113.         /// </summary>
  114.         /// <param name="a">BaseX number to convert</param>
  115.         /// <param name="b">Integer to add</param>
  116.         /// <param name="baseAlphabet">BaseX alphabet to use</param>
  117.         /// <returns>Base36 result</returns>
  118.         public static string Add(string a, Int64 b, string baseAlphabet)
  119.         {
  120.             return FromInt64(ToInt64(a, baseAlphabet) + b, baseAlphabet);
  121.         }
  122.  
  123.         /// <summary>
  124.         /// Adds a specified BaseX number to a BaseX number
  125.         /// </summary>
  126.         /// <param name="a">BaseX number to convert</param>
  127.         /// <param name="b">BaseX number to add</param>
  128.         /// <param name="baseAlphabet">BaseX alphabet to use</param>
  129.         /// <returns>Base36 result</returns>
  130.         public static string Add(string a, string b, string baseAlphabet)
  131.         {
  132.             return FromInt64(ToInt64(a, baseAlphabet) + ToInt64(b, baseAlphabet), baseAlphabet);
  133.         }
  134.  
  135.         /// <summary>
  136.         /// Subtracts a specified integer from a BaseX number
  137.         /// </summary>
  138.         /// <param name="a">BaseX number to convert</param>
  139.         /// <param name="b">Integer to subtract</param>
  140.         /// <param name="baseAlphabet">BaseX alphabet to use</param>
  141.         /// <returns>Base36 result</returns>
  142.         public static string Subtract(string a, Int64 b, string baseAlphabet)
  143.         {
  144.             return FromInt64(ToInt64(a, baseAlphabet) - b, baseAlphabet);
  145.         }
  146.  
  147.         /// <summary>
  148.         /// Subtracts a specified BaseX number from a BaseX number
  149.         /// </summary>
  150.         /// <param name="a">BaseX number to convert</param>
  151.         /// <param name="b">BaseX number to subtract</param>
  152.         /// <param name="baseAlphabet">BaseX alphabet to use</param>
  153.         /// <returns>Base36 result</returns>
  154.         public static string Subtract(string a, string b, string baseAlphabet)
  155.         {
  156.             return FromInt64(ToInt64(a, baseAlphabet) - ToInt64(b, baseAlphabet), baseAlphabet);
  157.         }
  158.  
  159.         /// <summary>
  160.         /// Multiplies a specified integer and a BaseX number
  161.         /// </summary>
  162.         /// <param name="a">BaseX number to convert</param>
  163.         /// <param name="b">Integer to subtract</param>
  164.         /// <param name="baseAlphabet">BaseX alphabet to use</param>
  165.         /// <returns>Base36 result</returns>
  166.         public static string Multiply(string a, Int64 b, string baseAlphabet)
  167.         {
  168.             return FromInt64(ToInt64(a, baseAlphabet) * b, baseAlphabet);
  169.         }
  170.  
  171.         /// <summary>
  172.         /// Multiplies a specified BaseX number and a BaseX number
  173.         /// </summary>
  174.         /// <param name="a">BaseX number to convert</param>
  175.         /// <param name="b">BaseX number to subtract</param>
  176.         /// <param name="baseAlphabet">BaseX alphabet to use</param>
  177.         /// <returns>Base36 result</returns>
  178.         public static string Multiply(string a, string b, string baseAlphabet)
  179.         {
  180.             return FromInt64(ToInt64(a, baseAlphabet) * ToInt64(b, baseAlphabet), baseAlphabet);
  181.         }
  182.  
  183.         /// <summary>
  184.         /// Divides a BaseX number by a specified integer (without remainder)
  185.         /// </summary>
  186.         /// <param name="a">BaseX number to convert</param>
  187.         /// <param name="b">Integer to subtract</param>
  188.         /// <param name="baseAlphabet">BaseX alphabet to use</param>
  189.         /// <returns>Base36 result</returns>
  190.         public static string Divide(string a, Int64 b, string baseAlphabet)
  191.         {
  192.             return FromInt64(ToInt64(a, baseAlphabet) / b, baseAlphabet);
  193.         }
  194.  
  195.         /// <summary>
  196.         /// Divides a BaseX number by a specified BaseX number (without remainder)
  197.         /// </summary>
  198.         /// <param name="a">BaseX number to convert</param>
  199.         /// <param name="b">BaseX number to subtract</param>
  200.         /// <param name="baseAlphabet">BaseX alphabet to use</param>
  201.         /// <returns>Base36 result</returns>
  202.         public static string Divide(string a, string b, string baseAlphabet)
  203.         {
  204.             return FromInt64(ToInt64(a, baseAlphabet) / ToInt64(b, baseAlphabet), baseAlphabet);
  205.         }
  206.     }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement