Interpret numeric values as being hexadecimal values if they contain hex digits a-f
authorCasper Hornstrup <chorns@users.sourceforge.net>
Wed, 16 Feb 2005 23:17:17 +0000 (23:17 +0000)
committerCasper Hornstrup <chorns@users.sourceforge.net>
Wed, 16 Feb 2005 23:17:17 +0000 (23:17 +0000)
svn path=/trunk/; revision=13612

irc/TechBot/TechBot.Library/NumberParser.cs

index 9d4eab5..15521ed 100644 (file)
@@ -7,13 +7,43 @@ namespace TechBot.Library
        {\r
                public bool Error = false;\r
                \r
        {\r
                public bool Error = false;\r
                \r
+               private const string SpecialHexCharacters = "ABCDEF";\r
+\r
+               private bool IsSpecialHexCharacter(char ch)\r
+               {\r
+                       foreach (char specialChar in SpecialHexCharacters)\r
+                       {\r
+                               if (ch.ToString().ToUpper() == specialChar.ToString())\r
+                                       return true;\r
+                       }\r
+                       return false;\r
+               }\r
+\r
+               private bool HasSpecialHexCharacters(string s)\r
+               {\r
+                       foreach (char ch in s)\r
+                       {\r
+                               if (IsSpecialHexCharacter(ch))\r
+                                       return true;\r
+                       }\r
+                       return false;\r
+               }\r
+               \r
                public long Parse(string s)\r
                {\r
                        try\r
                        {\r
                                Error = false;\r
                public long Parse(string s)\r
                {\r
                        try\r
                        {\r
                                Error = false;\r
+                               bool useHex = false;\r
                                if (s.StartsWith("0x"))\r
                                if (s.StartsWith("0x"))\r
-                                       return Int64.Parse(s.Substring(2),\r
+                               {\r
+                                       s = s.Substring(2);\r
+                                       useHex = true;\r
+                               }\r
+                               if (HasSpecialHexCharacters(s))\r
+                                       useHex = true;\r
+                               if (useHex)\r
+                                       return Int64.Parse(s,\r
                                                           NumberStyles.HexNumber);\r
                                else\r
                                        return Int64.Parse(s);\r
                                                           NumberStyles.HexNumber);\r
                                else\r
                                        return Int64.Parse(s);\r