System.Convert.Convert.ConvertFromBase C# (CSharp) Method

ConvertFromBase() private static method

private static ConvertFromBase ( string value, int fromBase, bool unsigned ) : int
value string
fromBase int
unsigned bool
return int
		private static int ConvertFromBase (string value, int fromBase, bool unsigned)
		{
			if (NotValidBase (fromBase))
				throw new ArgumentException ("fromBase is not valid.");
			if (value == null)
				return 0;

			int chars = 0;
			int result = 0;
			int digitValue;

			int i=0; 
			int len = value.Length;
			bool negative = false;

			// special processing for some bases
			switch (fromBase) {
				case 10:
					if (value.Substring (i, 1) == "-") {
						if (unsigned) {
							throw new OverflowException (
								Locale.GetText ("The string was being parsed as"
								+ " an unsigned number and could not have a"
								+ " negative sign."));
						}
						negative = true;
						i++;
					}
					break;
				case 16:
					if (value.Substring (i, 1) == "-") {
						throw new ArgumentException ("String cannot contain a "
							+ "minus sign if the base is not 10.");
					}
					if (len >= i + 2) {
						// 0x00 or 0X00
						if ((value[i] == '0') && ((value [i+1] == 'x') || (value [i+1] == 'X'))) {
							i+=2;
						}
					}
					break;
				default:
					if (value.Substring (i, 1) == "-") {
						throw new ArgumentException ("String cannot contain a "
							+ "minus sign if the base is not 10.");
					}
					break;
			}

			if (len == i) {
				throw new FormatException ("Could not find any parsable digits.");
			}

			if (value[i] == '+') {
				i++;
			}

			while (i < len) {
				char c = value[i++];
				if (Char.IsNumber (c)) {
					digitValue = c - '0';
				} else if (Char.IsLetter (c)) {
					digitValue = Char.ToLowerInvariant (c) - 'a' + 10;
				} else {
					if (chars > 0) {
						throw new FormatException ("Additional unparsable "
							+ "characters are at the end of the string.");
					} else {
						throw new FormatException ("Could not find any parsable"
							+ " digits.");
					}
				}

				if (digitValue >= fromBase) {
					if (chars > 0) {
						throw new FormatException ("Additional unparsable "
							+ "characters are at the end of the string.");
					} else {
						throw new FormatException ("Could not find any parsable"
							+ " digits.");
					}
				}

				result = (fromBase) * result + digitValue;
				chars ++;
			}

			if (chars == 0)
				throw new FormatException ("Could not find any parsable digits.");

			if (negative)
				return -result;
			else
				return result;
		}