Example #1
0
        public static string ConvertToBaseN(ulong Number, int TargetBase, CharAlphabet alphabet = null, int MinDigits = 1, int sign = 0)
        {
            //Exceptions
            if (alphabet.Chars.Count() < TargetBase)
            {
                throw new ArgumentException(LangManager.GetString("exception_alphabet_too_short", CurrentCulture));
            }

            if (Number < 0)
            {
                sign = -1;
            }

            //Defaults
            if (alphabet == null)
            {
                alphabet = Alphabet.Hexadecimal;
            }

            //Init
            string result      = "";
            ulong  placeholder = Number;

            //Conversion
            if (Number == 0)
            {
                sign   = 0;
                result = result.Insert(0, alphabet.Chars[0].ToString());
            }
            else
            {
                while (placeholder != 0)
                {
                    result      += alphabet.Chars[(int)placeholder % TargetBase];
                    placeholder -= placeholder % (ulong)TargetBase;
                    placeholder /= (ulong)TargetBase;
                }
            }

            //Add mindigits
            if (result.Length < MinDigits)
            {
                for (int x = result.Length; x < MinDigits; x++)
                {
                    result = result.Insert(0, alphabet.Chars[0].ToString());
                }
            }

            return(StringManipulator.Reverse(result));
        }