CSharp examples for System:Math Number
Convert any positive number from base 2 till 16 to 10 base number
using System.Text; using System.Collections.Generic; using System;//from ww w. j a v a2s. c o m public class Main{ /// <summary> /// Convert any positive number from base 2 till 16 to 10 base number /// </summary> public static long FromAnyTo10(string number, int baseFrom) { if (number[0] == '-') { throw new ArgumentException("I can't work with negative numbers!"); } else if (baseFrom < 2 || baseFrom > 16) { throw new ArgumentException("I can't work with numbers with base smaller than 2 and greater than 16!"); } int[] digits = new int[number.Length]; for (int i = 0; i < digits.Length; i++) { int temp = 0; switch (number[i]) { case 'A': temp = 10; break; case 'B': temp = 11; break; case 'C': temp = 12; break; case 'D': temp = 13; break; case 'E': temp = 14; break; case 'F': temp = 15; break; default: temp = int.Parse(number[i].ToString()); break; } digits[i] = temp; } long result = 0; int power = number.Length - 1; for (int i = 0; i < digits.Length; i++) { result += digits[i] * Numbers.NumberToPower(baseFrom, power); power--; } return result; } }