2005-01-15 19:27:25 +00:00
|
|
|
using System;
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
|
|
namespace TechBot.Library
|
|
|
|
{
|
|
|
|
public class NumberParser
|
|
|
|
{
|
|
|
|
public bool Error = false;
|
|
|
|
|
2005-02-16 23:17:17 +00:00
|
|
|
private const string SpecialHexCharacters = "ABCDEF";
|
|
|
|
|
2006-09-09 10:53:28 +00:00
|
|
|
private static bool IsSpecialHexCharacter(char ch)
|
2005-02-16 23:17:17 +00:00
|
|
|
{
|
|
|
|
foreach (char specialChar in SpecialHexCharacters)
|
|
|
|
{
|
|
|
|
if (ch.ToString().ToUpper() == specialChar.ToString())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-09-09 10:53:28 +00:00
|
|
|
private static bool HasSpecialHexCharacters(string s)
|
2005-02-16 23:17:17 +00:00
|
|
|
{
|
|
|
|
foreach (char ch in s)
|
|
|
|
{
|
|
|
|
if (IsSpecialHexCharacter(ch))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-01-15 19:27:25 +00:00
|
|
|
public long Parse(string s)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Error = false;
|
2005-02-16 23:17:17 +00:00
|
|
|
bool useHex = false;
|
2006-09-09 10:53:28 +00:00
|
|
|
if (s.StartsWith("0x", StringComparison.InvariantCultureIgnoreCase))
|
2005-02-16 23:17:17 +00:00
|
|
|
{
|
|
|
|
s = s.Substring(2);
|
|
|
|
useHex = true;
|
|
|
|
}
|
|
|
|
if (HasSpecialHexCharacters(s))
|
|
|
|
useHex = true;
|
|
|
|
if (useHex)
|
|
|
|
return Int64.Parse(s,
|
2005-01-15 19:27:25 +00:00
|
|
|
NumberStyles.HexNumber);
|
|
|
|
else
|
|
|
|
return Int64.Parse(s);
|
|
|
|
}
|
|
|
|
catch (FormatException)
|
|
|
|
{
|
|
|
|
Error = true;
|
|
|
|
}
|
|
|
|
catch (OverflowException)
|
|
|
|
{
|
|
|
|
Error = true;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|