Aqueduct.Utils.BaseConverter.ToBase10 C# (CSharp) Метод

ToBase10() публичный статический Метод

Converts a number from any base between 2 and 36 to base 10.
public static ToBase10 ( string encodedNumber, ushort fromBase ) : ulong
encodedNumber string The number to be converted.
fromBase ushort The base from which to convert. Has to be between 2 and 36.
Результат ulong
		public static ulong ToBase10(string encodedNumber, ushort fromBase)
		{
			// If the from base is 10, simply parse the string and return it
			if (fromBase == 10)
			{
				return UInt64.Parse(encodedNumber);
			}

			// Ensure that the string only contains upper case characters
			encodedNumber = encodedNumber.ToUpper();

			// Go through each character and decode its value.
			int length = encodedNumber.Length;
			ulong runningTotal = 0;

			for (int index = 0; index < length; index++)
			{
				char currentChar = encodedNumber[index];

				// Anything above base 10 uses letters as well as numbers, so A will be 10, B will be 11, etc.
				uint currentValue = Char.IsDigit(currentChar) ? (uint)(currentChar - '0') :
																(uint)(currentChar - 'A' + 10);

				// The value which of the character represents depends on its position and it is calculated
				// by multiplying its value with the power of the base to the position of the character, from
				// right to left.
				runningTotal += currentValue * (ulong)Math.Pow(fromBase, length - index - 1);
			}

			return runningTotal;
		}

Usage Example

Пример #1
0
        /// <summary>
        ///		Converts a number from any base between 2 and 36 to any base between 2 and 36.
        /// </summary>
        /// <param name="numberAsString">The number to be converted.</param>
        /// <param name="fromBase">The base from which to convert. Has to be between 2 and 36.</param>
        /// <param name="toBase">The base to which to convert. Has to be between 2 and 36.</param>
        /// <returns>The number converted from fromBase to toBase.</returns>
        public static string ToBase(string numberAsString, ushort fromBase, ushort toBase)
        {
            ulong base10 = BaseConverter.ToBase10(numberAsString, fromBase);

            return(BaseConverter.FromBase10(base10, toBase));
        }