System.ParseNumbers.StringToLong C# (CSharp) Method

StringToLong() public static method

public static StringToLong ( string value, int fromBase, int flags, int parsePos ) : long
value string
fromBase int
flags int
parsePos int
return long
		public unsafe static long StringToLong (string value, int fromBase, int flags, int* parsePos)
		{
			if ((flags & (IsTight | NoSpace)) == 0)
				throw new NotImplementedException (flags.ToString ());

			if (value == null)
				return 0;

			int chars = 0;
			int digitValue = -1;
			long result = 0;

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

			if (len == 0) {
				// Mimic broken .net behaviour
				throw new ArgumentOutOfRangeException ("Empty string");
			}

			int i = parsePos == null ? 0 : *parsePos;

			//Check for a sign
			if (value [i] == '-') {
				if (fromBase != 10)
					throw new ArgumentException ("String cannot contain a minus sign if the base is not 10.");

				if ((flags & TreatAsUnsigned) != 0)
					throw new OverflowException ("Negative number");

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

			if (fromBase == 16 && i + 1 < len && value [i] =='0' && (value [i + 1] == 'x' || value [i + 1] == 'X')) {
				i += 2;
			}

			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 (i == 0)
						throw new FormatException ("Could not find any parsable digits.");

					if ((flags & IsTight) != 0)
						throw new FormatException ("Additional unparsable characters are at the end of the string.");

					break;
				}

				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++;
				++i;
			}

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

			if (parsePos != null)
				*parsePos = i;

			return negative ? -result : result;
		}

Same methods

ParseNumbers::StringToLong ( string value, int fromBase, int flags ) : long

Usage Example

Beispiel #1
0
 public unsafe static long StringToLong(string s, int radix, int flags, ref int currPos)
 {
     fixed(int *ptr = &currPos)
     {
         return(ParseNumbers.StringToLong(s, radix, flags, ptr));
     }
 }
All Usage Examples Of System.ParseNumbers::StringToLong