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

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

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

			// The number has to be divided by the base it needs to be converted to
			// until the result of the division is 0. The modulus of the division 
			// is used to calculate the character that represents it
			StringBuilder runningResult = new StringBuilder();

			while (number > 0)
			{
				ulong modulus = number % toBase;

				if (modulus < 10)
				{
					runningResult.Insert(0, modulus);
				}
				else
				{
					runningResult.Insert(0, (char)('A' + modulus - 10));
				}

				number = (number - modulus) / toBase;
			}

			return runningResult.ToString();
		}
	}

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