Skip to content

Commit

Permalink
Optimized HexToBytes function (#469)
Browse files Browse the repository at this point in the history
- used Span
- used Convert.FromHexString vectorized function
  • Loading branch information
irodai-majom authored Oct 30, 2024
1 parent 6e92acf commit 9c49fce
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/N_m3u8DL-RE.Common/Util/HexUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,13 @@ public static bool TryParseHexString(string input, out byte[]? bytes)

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);
}
}
}

0 comments on commit 9c49fce

Please sign in to comment.