From a1c0271111f54e9c38d80a5b1d1bb4a34a705af9 Mon Sep 17 00:00:00 2001 From: Casper Hornstrup Date: Wed, 16 Feb 2005 23:17:17 +0000 Subject: [PATCH] Interpret numeric values as being hexadecimal values if they contain hex digits a-f svn path=/trunk/; revision=13612 --- irc/TechBot/TechBot.Library/NumberParser.cs | 32 ++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/irc/TechBot/TechBot.Library/NumberParser.cs b/irc/TechBot/TechBot.Library/NumberParser.cs index 9d4eab5c7e7..15521ede73b 100644 --- a/irc/TechBot/TechBot.Library/NumberParser.cs +++ b/irc/TechBot/TechBot.Library/NumberParser.cs @@ -7,13 +7,43 @@ namespace TechBot.Library { public bool Error = false; + private const string SpecialHexCharacters = "ABCDEF"; + + private bool IsSpecialHexCharacter(char ch) + { + foreach (char specialChar in SpecialHexCharacters) + { + if (ch.ToString().ToUpper() == specialChar.ToString()) + return true; + } + return false; + } + + private bool HasSpecialHexCharacters(string s) + { + foreach (char ch in s) + { + if (IsSpecialHexCharacter(ch)) + return true; + } + return false; + } + public long Parse(string s) { try { Error = false; + bool useHex = false; if (s.StartsWith("0x")) - return Int64.Parse(s.Substring(2), + { + s = s.Substring(2); + useHex = true; + } + if (HasSpecialHexCharacters(s)) + useHex = true; + if (useHex) + return Int64.Parse(s, NumberStyles.HexNumber); else return Int64.Parse(s);