Optimized HexToBytes function (#469)

- used Span
- used Convert.FromHexString vectorized function
This commit is contained in:
irodai-majom 2024-10-30 15:50:42 +01:00 committed by GitHub
parent 6e92acfda9
commit 9c49fce4ff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 6 additions and 8 deletions

View File

@ -34,15 +34,13 @@ namespace N_m3u8DL_RE.Common.Util
public static byte[] HexToBytes(string hex)
{
hex = hex.Trim();
if (hex.StartsWith("0x") || hex.StartsWith("0X"))
hex = hex.Substring(2);
byte[] bytes = new byte[hex.Length / 2];
var hexSpan = hex.AsSpan().Trim();
if (hexSpan.StartsWith("0x") || hexSpan.StartsWith("0X"))
{
hexSpan = hexSpan.Slice(2);
}
for (int i = 0; i < hex.Length; i += 2)
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
return bytes;
return Convert.FromHexString(hexSpan);
}
}
}