From d82f49e513f7c83cee15c9c3feb83ac0b46e04e5 Mon Sep 17 00:00:00 2001 From: Kurt Date: Tue, 31 Mar 2020 19:44:32 -0700 Subject: [PATCH] Fix offset parse oops hex dec --- NHSE.Core/Util/StringUtil.cs | 37 ++++++++++++++++++++++++++++++ NHSE.WinForms/Subforms/SysBotUI.cs | 7 ++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/NHSE.Core/Util/StringUtil.cs b/NHSE.Core/Util/StringUtil.cs index 37f12a0..3251441 100644 --- a/NHSE.Core/Util/StringUtil.cs +++ b/NHSE.Core/Util/StringUtil.cs @@ -42,5 +42,42 @@ public static string CleanFileName(string fileName) { return string.Concat(fileName.Split(Path.GetInvalidFileNameChars())); } + + + /// + /// Parses the hex string into a , skipping all characters except for valid digits. + /// + /// Hex String to parse + /// Parsed value + public static uint GetHexValue(string value) + { + uint result = 0; + if (string.IsNullOrEmpty(value)) + return result; + + foreach (var c in value) + { + if (IsNum(c)) + { + result <<= 4; + result += (uint)(c - '0'); + } + else if (IsHexUpper(c)) + { + result <<= 4; + result += (uint)(c - 'A' + 10); + } + else if (IsHexLower(c)) + { + result <<= 4; + result += (uint)(c - 'a' + 10); + } + } + return result; + } + + private static bool IsNum(char c) => (uint)(c - '0') <= 9; + private static bool IsHexUpper(char c) => (uint)(c - 'A') <= 5; + private static bool IsHexLower(char c) => (uint)(c - 'a') <= 5; } } \ No newline at end of file diff --git a/NHSE.WinForms/Subforms/SysBotUI.cs b/NHSE.WinForms/Subforms/SysBotUI.cs index 0acf662..02269b9 100644 --- a/NHSE.WinForms/Subforms/SysBotUI.cs +++ b/NHSE.WinForms/Subforms/SysBotUI.cs @@ -1,5 +1,6 @@ using System; using System.Windows.Forms; +using NHSE.Core; using NHSE.Injection; using NHSE.WinForms.Properties; @@ -88,7 +89,8 @@ private void B_Connect_Click(object sender, EventArgs e) private void B_WriteCurrent_Click(object sender, EventArgs e) { - if (!uint.TryParse(RamOffset.Text, out var offset)) + var offset = StringUtil.GetHexValue(RamOffset.Text); + if (offset == 0) { WinFormsUtil.Error("Incorrect hex offset."); return; @@ -110,7 +112,8 @@ private void B_WriteCurrent_Click(object sender, EventArgs e) private void B_ReadCurrent_Click(object sender, EventArgs e) { - if (!uint.TryParse(RamOffset.Text, out var offset)) + var offset = StringUtil.GetHexValue(RamOffset.Text); + if (offset == 0) { WinFormsUtil.Error("Incorrect hex offset."); return;