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)
|
public static byte[] HexToBytes(string hex)
|
||||||
{
|
{
|
||||||
hex = hex.Trim();
|
var hexSpan = hex.AsSpan().Trim();
|
||||||
if (hex.StartsWith("0x") || hex.StartsWith("0X"))
|
if (hexSpan.StartsWith("0x") || hexSpan.StartsWith("0X"))
|
||||||
hex = hex.Substring(2);
|
{
|
||||||
byte[] bytes = new byte[hex.Length / 2];
|
hexSpan = hexSpan.Slice(2);
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < hex.Length; i += 2)
|
return Convert.FromHexString(hexSpan);
|
||||||
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
|
|
||||||
|
|
||||||
return bytes;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue