/// <summary>
        /// Initializes a new instance of the <see cref="FormattedInteger"/> class.
        /// </summary>
        /// <param name="integerBase">The base.</param>
        /// <param name="sign">The optional sign.</param>
        /// <param name="leadingZeroCount">The number of leading zeroes.</param>
        /// <param name="integerText">The integer text..</param>
        /// <param name="invalidText">The trailing invalid text, if any.</param>
        /// <param name="canonical">The canonical form of the number.</param>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="leadingZeroCount"/> is lesser than zero.</exception>
        internal FormattedInteger(IIntegerBase integerBase, OptionalSign sign, int leadingZeroCount, string integerText, string invalidText, CanonicalNumber canonical)
            : base(invalidText, canonical)
        {
            IntegerBase      = integerBase;
            Sign             = sign;
            LeadingZeroCount = leadingZeroCount;
            IntegerText      = integerText;

            if (leadingZeroCount < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(leadingZeroCount));
            }
        }
예제 #2
0
        private static string ConvertFromBinary(string text, IIntegerBase toBase)
        {
            Debug.Assert(!string.IsNullOrEmpty(text));

            string Result = "0";

            for (int i = 0; i < text.Length; i++)
            {
                bool AddCarry = text[i] != '0';
                Result = toBase.MultipliedByTwo(Result, AddCarry);
            }

            return(Result);
        }
예제 #3
0
        private static string ConvertToBinary(string text, IIntegerBase fromBase)
        {
            Debug.Assert(!string.IsNullOrEmpty(text));

            string Number = text;
            string Result = string.Empty;

            do
            {
                Number = fromBase.DividedByTwo(Number, out bool HasCarry);
                Result = (HasCarry ? "1" : "0") + Result;
            }while (Number != "0");

            return(Result);
        }
예제 #4
0
 /// <summary>
 /// Returns the value of <paramref name="text"/> converted to a new base.
 /// </summary>
 /// <param name="text">The number to convert.</param>
 /// <param name="fromBase">The base in which <paramref name="text"/> is encoded.</param>
 /// <param name="toBase">The base in which the returned number is encoded.</param>
 public static string Convert(string text, IIntegerBase fromBase, IIntegerBase toBase)
 {
     return(ConvertFromBinary(ConvertToBinary(text, fromBase), toBase));
 }