Use new IO byte array extensions

This commit is contained in:
Matt Nadareski
2024-10-24 00:46:28 -04:00
parent 15dba6fbbd
commit c6109fdf97
11 changed files with 52 additions and 99 deletions

View File

@@ -1,4 +1,3 @@
using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
@@ -8,56 +7,6 @@ namespace SabreTools.Core.Tools
{
public static class TextHelper
{
#region Conversion
/// <summary>
/// Convert a byte array to a hex string
/// </summary>
public static string? ByteArrayToString(byte[]? bytes)
{
// If we get null in, we send null out
if (bytes == null)
return null;
try
{
string hex = BitConverter.ToString(bytes);
return hex.Replace("-", string.Empty).ToLowerInvariant();
}
catch
{
return null;
}
}
/// <summary>
/// Convert a hex string to a byte array
/// </summary>
public static byte[]? StringToByteArray(string? hex)
{
// If we get null in, we send null out
if (string.IsNullOrEmpty(hex))
return null;
try
{
int NumberChars = hex!.Length;
byte[] bytes = new byte[NumberChars / 2];
for (int i = 0; i < NumberChars; i += 2)
{
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
}
return bytes;
}
catch
{
return null;
}
}
#endregion
#region Normalization
/// <summary>