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

ConvertFromBase64() private static method

private static ConvertFromBase64 ( string value, int fromBase, bool unsigned ) : long
value string
fromBase int
unsigned bool
return long
		private static long ConvertFromBase64 (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 digitValue = -1;
			long result = 0;
			bool negative = false;

			int i = 0;
			int len = value.Length;

			// 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 -1 * result;
			else
				return result;
		}