LoriBru
1/14/2019 - 10:48 AM

HEX string to byte array

This method converts a hex string to a byte array.

public static byte[] StringToByteArray(string hex)
{
  return Enumerable.Range(0, hex.Length)
                   .Where(x => x % 2 == 0)
                   .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
                   .ToArray();
}