Fix offset parse

oops hex dec
This commit is contained in:
Kurt
2020-03-31 19:44:32 -07:00
parent f18b013a88
commit d82f49e513
2 changed files with 42 additions and 2 deletions

View File

@@ -42,5 +42,42 @@ public static string CleanFileName(string fileName)
{
return string.Concat(fileName.Split(Path.GetInvalidFileNameChars()));
}
/// <summary>
/// Parses the hex string into a <see cref="uint"/>, skipping all characters except for valid digits.
/// </summary>
/// <param name="value">Hex String to parse</param>
/// <returns>Parsed value</returns>
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;
}
}

View File

@@ -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;