<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="10.0.0-preview.2.25163.2" />

HexConverter

static class HexConverter
using System.Runtime.CompilerServices; namespace System { internal static class HexConverter { public enum Casing : uint { Upper = 0, Lower = 8224 } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void ToCharsBuffer(byte value, Span<char> buffer, int startingIndex = 0, Casing casing = Casing.Upper) { uint num = (uint)(((value & 240) << 4) + (value & 15) - 35209); uint num2 = (uint)((int)((((0 - num) & 28784) >> 4) + num + 47545) | (int)casing); buffer[startingIndex + 1] = (char)(num2 & 255); buffer[startingIndex] = (char)(num2 >> 8); } public static void EncodeToUtf16(ReadOnlySpan<byte> bytes, Span<char> chars, Casing casing = Casing.Upper) { for (int i = 0; i < bytes.Length; i++) { ToCharsBuffer(bytes[i], chars, i * 2, casing); } } public unsafe static string ToString(ReadOnlySpan<byte> bytes, Casing casing = Casing.Upper) { return string.Create(bytes.Length * 2, ((IntPtr)(&bytes), casing), delegate(Span<char> chars, (IntPtr RosPtr, Casing casing) args) { EncodeToUtf16(*(ReadOnlySpan<byte>*)(long)args.RosPtr, chars, args.casing); }); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int FromLowerChar(int c) { if ((uint)(c - 48) <= 9) return c - 48; if ((uint)(c - 97) <= 5) return c - 97 + 10; return 255; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool IsHexLowerChar(int c) { if ((uint)(c - 48) > 9) return (uint)(c - 97) <= 5; return true; } } }