<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="10.0.0-rc.2.25502.107" />

HexConverter

static class HexConverter
using System.Runtime.CompilerServices; namespace System { internal static class HexConverter { public enum Casing : uint { Upper = 0, Lower = 8224 } public unsafe static ReadOnlySpan<byte> CharToHexLookup => new ReadOnlySpan<byte>(&global::<PrivateImplementationDetails>.21244F82B210125632917591768F6BF22EB6861F80C6C25A25BD26DFB580EA7B, 256); [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> source, Span<char> destination, Casing casing = Casing.Upper) { for (int i = 0; i < source.Length; i++) { ToCharsBuffer(source[i], destination, 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 FromChar(int c) { ReadOnlySpan<byte> charToHexLookup = CharToHexLookup; if (c < charToHexLookup.Length) { charToHexLookup = CharToHexLookup; return charToHexLookup[c]; } return 255; } [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; } } }