public static long ParseInt64(string str, NumberEncoding style) { if (style == NumberEncoding.Hexadecimal) { return(ParseInt64Hex(str)); } return(ParseInt64(str)); }
/// <summary> /// Attempts to parse a number into a Uint32 /// </summary> /// <param name="str"></param> /// <param name="style"></param> /// <param name="result"></param> /// <returns></returns> public static bool TryParseUInt32(string str, NumberEncoding style, out UInt32 result) { bool sign; ulong tmp; bool bresult = Helper.TryParseUInt64Core(str, style == NumberEncoding.Hexadecimal ? true : false, out tmp, out sign); result = (UInt32)tmp; return(bresult && !sign); }
/// <summary> /// Determines the type of number (int, double, etc) and returns an object /// containing that value. /// </summary> /// <param name="json"></param> /// <param name="index"></param> /// <param name="success"></param> /// <returns></returns> protected static object ParseNumber(char[] json, ref int index, ref bool success) { EatWhitespace(json, ref index); int lastIndex = GetLastIndexOfNumber(json, index); int charLength = (lastIndex - index) + 1; // We now have the number as a string. Parse it to determine the type of number. string value = new string(json, index, charLength); // Since the Json doesn't contain the Type of the property, and since multiple number // values can fit in the various Types (e.g. 33 decimal fits in an Int16, UInt16, // Int32, UInt32, Int64, and UInt64), we need to be a bit smarter in how we deal with // the size of the number, and also the case (negative or positive). object result = null; string dot = CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator; string comma = CultureInfo.CurrentUICulture.NumberFormat.NumberGroupSeparator; string minus = CultureInfo.CurrentUICulture.NumberFormat.NegativeSign; string plus = CultureInfo.CurrentUICulture.NumberFormat.PositiveSign; if (value.Contains(dot) || value.Contains(comma) || value.Contains("e") || value.Contains("E")) { // We have either a double or a float. Force it to be a double // and let the deserializer unbox it into the proper size. result = Double.Parse(new string(json, index, charLength)); } else { NumberEncoding style = NumberEncoding.Decimal; if (value.StartsWith("0x") || (value.IndexOfAny(new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'A', 'B', 'C', 'D', 'E', 'F' }) >= 0)) { style = NumberEncoding.Hexadecimal; } result = Utils.ParseInt64(value, style); } index = lastIndex + 1; return(result); }
public static MSFrameworkBuilder UseNumberEncoding(this MSFrameworkBuilder builder, string path = "number_encoding.txt") { string codes; if (!File.Exists(path)) { codes = NumberEncoding.GetRandomCodes(); File.WriteAllText(path, codes); } else { codes = File.ReadAllLines(path).FirstOrDefault(); } if (string.IsNullOrWhiteSpace(codes) || codes.Length < 34) { throw new ArgumentException("Codes show large than 34 char"); } NumberEncoding.Load(codes); return(builder); }