Move more helper methods around

This commit is contained in:
Matt Nadareski
2023-08-14 13:36:37 -04:00
parent b37aed389e
commit 2e662c0b4e
15 changed files with 145 additions and 144 deletions

View File

@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
@@ -7,6 +8,58 @@ 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.IsNullOrWhiteSpace(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>
/// Normalize a string to the WoD standard
/// </summary>
@@ -75,6 +128,8 @@ namespace SabreTools.Core.Tools
return new string(input.Where(c => !invalidPath.Contains(c)).ToArray());
}
#endregion
#region Helpers
/// <summary>