Optimized HexToBytes function (#469)
- used Span - used Convert.FromHexString vectorized function
This commit is contained in:
parent
6e92acfda9
commit
9c49fce4ff
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue