diff --git a/Refresh.Common/Helpers/HexHelper.cs b/Refresh.Common/Helpers/HexHelper.cs index a77aedfd..a7e7ea18 100644 --- a/Refresh.Common/Helpers/HexHelper.cs +++ b/Refresh.Common/Helpers/HexHelper.cs @@ -5,7 +5,7 @@ namespace Refresh.Common.Helpers; public static class HexHelper { /// - /// Converts a byte array to a hex string + /// Converts a byte array to a hex string /// /// The hex bytes /// The hex string @@ -22,44 +22,44 @@ public static string BytesToHexString(ReadOnlySpan data) return new string(hexChars); } - + /// - /// Converts a hex string to a byte array + /// Converts a hex string to a byte array /// /// /// /// [MethodImpl(MethodImplOptions.AggressiveOptimization)] - public static byte[] HexStringToBytes(string hex) { + public static byte[] HexStringToBytes(string hex) + { if (hex.Length % 2 != 0) throw new FormatException("The hex string is invalid as it contains an odd number of bytes."); - + // Two hex digits per byte byte[] arr = new byte[hex.Length / 2]; - + for (int i = 0; i < arr.Length; ++i) - { // The bitmasks may seem redundant, but from my testing it consistently improves perf by ~10%, // probably because the compiler/JIT knows it can omit range checks in the byte -> int cast arr[i] = (byte)(((GetHexVal(hex[i * 2]) << 4) & 0xF0) + (GetHexVal(hex[i * 2 + 1]) & 0x0F)); - } - + return arr; } - + /// - /// Gets the 4 bit value of the single hex digit + /// Gets the 4 bit value of the single hex digit /// /// The hex digit /// The 4 bit value of the digit [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - private static int GetHexVal(char hex) { + private static int GetHexVal(char hex) + { int val = hex; return val - (val < 58 ? 48 : 87); } /// - /// Gets the hex digit for a 4-bit value + /// Gets the hex digit for a 4-bit value /// /// The 4 bit value /// The hex digit