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); }
/// <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"); } }
/// <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); }