Example #1
0
        public List <string> ConvertToDigitListAndRemoveEmptyDigits(string number, int radix, out int delimeterIndex)
        {
            var value     = RemoveDelimeter(number, radix, out delimeterIndex);
            var digitList = ConversionHelpers.RepresentationStringToListOfStrings(value, radix);

            digitList.RemoveAll(x => x == " ");
            return(digitList);
        }
Example #2
0
        /// <summary>
        /// Converts value string of specified base to decimal
        /// </summary>
        /// <param name="valueString"></param>
        /// <param name="radix"></param>
        /// <returns></returns>
        public double ArbitraryBaseToDecimal(string valueString, int radix)
        {
            if (IsValidRadix(radix))
            {
                if (IsValidString(valueString, radix))
                {
                    // Set the base of representation before converting
                    digits.CurrentBase = radix;

                    double result = 0.0;
                    int    mult   = 1;

                    // While converting, the numbers are assumed to be unsigned
                    // Detect and remember if number was negative
                    if (valueString.ElementAt(0) == '-')
                    {
                        valueString = valueString.Substring(1);
                        mult        = -1;
                    }

                    // Digits at positions in some representation are represented by multiple characters,
                    // so it's necessary to convert valueString to list of strings
                    var strList = ConversionHelpers.RepresentationStringToListOfStrings(valueString, radix);

                    // The value at each position is calculated by taking the value of digit
                    // and multiplying it by the base of number to the power of exponent

                    // The exponents at positions are as follows:
                    // For number 253
                    // Exponents:  ...2 1 0
                    // Digits:        5 3 1
                    // So the starting value of exponent is the count of elements in lis -1

                    int exponent = strList.Count - 1;
                    for (int i = 0; i <= exponent; i++)
                    {
                        // Decrease value of by 1 exponent after each iteration
                        result += (double)(digits.GetValue(strList.ElementAt(i)) * Math.Pow(radix, exponent - i));
                    }

                    // Make the number negative, if needed
                    return(result * mult);
                }
                else
                {
                    throw new System.ArgumentException("The characters in numberString " + valueString + " do not match the systemBase " + radix);
                }
            }
            else
            {
                throw new ArgumentException("The systemBase " + radix + " must be in range 2- 256");
            }
        }
Example #3
0
        /// <summary>
        /// Converts decimal fraction to it's string representation in specified base
        /// </summary>
        /// <param name="fraction">The fraction in decimal</param>
        /// <param name="radix">The radix</param>
        /// <returns></returns>
        public string DecimalFractionToArbitraryBase(double fraction, int radix)
        {
            if (fraction == 0.0)
            {
                if (radix <= 36)
                {
                    return("0");
                }
                return("00");
            }

            StringBuilder builder = new StringBuilder();

            digits.CurrentBase = radix;

            double number, fractionPart;
            int    wholePart;

            fractionPart = fraction;
            for (int i = 0; i < FormatPrecision; i++)
            {
                number       = fractionPart * radix;
                fractionPart = number % 1;
                wholePart    = (int)(number - fractionPart);

                if (wholePart < 1)
                {
                    wholePart *= -1;
                }

                string str = digits.GetDigit(wholePart);
                builder.Append(str);
                if (radix > 36)
                {
                    builder.Append(" ");
                }
            }
            return(ConversionHelpers.RemoveTrailingZeros(builder.ToString()));
        }
Example #4
0
        /// <summary>
        /// Converts string that represents fraction in specified base to it's value in decimal
        /// </summary>
        /// <param name="fractionStr"></param>
        /// <param name="radix"></param>
        /// <returns></returns>
        public double ArbitraryFractionToDecimal(string fractionStr, int radix)
        {
            double decimalFraction = 0.0;
            double exponent        = 1.0;

            var strList = ConversionHelpers.RepresentationStringToListOfStrings(fractionStr, radix);

            digits.CurrentBase = radix;

            // The value at each position is calculated by taking the value of digit
            // and multiplying it by the base of number to the power of exponent

            // The exponents at positions in fraction are as follows:
            // For fraction  0.253
            // Exponents:  0  . -1 -2 -3
            // Digits:     0  .  5  3  1

            for (int i = 0; i < strList.Count; i++)
            {
                decimalFraction += (double)(digits.GetValue(strList.ElementAt(i)) * Math.Pow(radix, exponent * -1));
                exponent++;
            }
            return(decimalFraction);
        }