Clean up `BytesToHexString`

This commit is contained in:
James Groom 2024-02-01 15:04:36 +00:00 committed by GitHub
parent f3aae71da6
commit 7e8950ea52
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 20 additions and 37 deletions

View File

@ -5,6 +5,7 @@ using System.Text;
using System.Windows.Forms;
using BizHawk.Client.Common;
using BizHawk.Common.BufferExtensions;
using BizHawk.Emulation.Common;
using BizHawk.Emulation.Cores.Nintendo.NES;
@ -48,15 +49,9 @@ namespace BizHawk.Client.EmuHawk
RegionComboBox.Items.AddRange(Enum.GetNames(typeof(NES.NESSyncSettings.Region)).Cast<object>().ToArray());
RegionComboBox.SelectedItem = Enum.GetName(typeof(NES.NESSyncSettings.Region), _syncSettings.RegionOverride);
if (_syncSettings.InitialWRamStatePattern != null && _syncSettings.InitialWRamStatePattern.Any())
if (_syncSettings.InitialWRamStatePattern is { Count: > 0 } initWRAMPattern)
{
var sb = new StringBuilder();
foreach (var b in _syncSettings.InitialWRamStatePattern)
{
sb.Append($"{b:X2}");
}
RamPatternOverrideBox.Text = sb.ToString();
RamPatternOverrideBox.Text = initWRAMPattern.BytesToHexString();
}
}

View File

@ -3,6 +3,8 @@ using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BizHawk.Common.BufferExtensions;
namespace BizHawk.Client.EmuHawk
{
public partial class HexFind : Form
@ -47,25 +49,10 @@ namespace BizHawk.Client.EmuHawk
private string GetFindBoxChars()
{
if (string.IsNullOrWhiteSpace(FindBox.Text))
{
return "";
}
if (HexRadio.Checked)
{
return FindBox.Text;
}
var bytes = _hexEditor.ConvertTextToBytes(FindBox.Text);
var byteString = new StringBuilder();
foreach (var b in bytes)
{
byteString.Append($"{b:X2}");
}
return byteString.ToString();
var text = FindBox.Text;
if (string.IsNullOrWhiteSpace(text)) return string.Empty;
if (HexRadio.Checked) return text;
return _hexEditor.ConvertTextToBytes(text).BytesToHexString();
}
private void Find_Prev_Click(object sender, EventArgs e)

View File

@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace BizHawk.Common.BufferExtensions
@ -50,20 +52,19 @@ namespace BizHawk.Common.BufferExtensions
}
}
/// <summary>
/// Converts bytes to an uppercase string of hex numbers in upper case without any spacing or anything
/// </summary>
public static string BytesToHexString(this byte[] bytes)
/// <summary>Creates a string containing the hexadecimal representation of <paramref name="bytes"/></summary>
/// <remarks>Output format is all-uppercase, no spaces, padded to an even number of nybbles, no prefix</remarks>
public static string BytesToHexString(this ReadOnlySpan<byte> bytes)
{
var sb = new StringBuilder();
foreach (var b in bytes)
{
sb.AppendFormat("{0:X2}", b);
}
StringBuilder sb = new(capacity: 2 * bytes.Length, maxCapacity: 2 * bytes.Length);
for (var i = 0; i < bytes.Length; i++) sb.AppendFormat("{0:X2}", bytes[i]);
return sb.ToString();
}
/// <inheritdoc cref="BytesToHexString(ReadOnlySpan{byte})"/>
public static string BytesToHexString(this IReadOnlyList<byte> bytes)
=> BytesToHexString(bytes is byte[] a ? a.AsSpan() : bytes.ToArray());
public static bool FindBytes(this byte[] array, byte[] pattern)
{
var fidx = 0;