I want to convert from hex string
to byte[]
and i have this function:
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();
}
When my string is for example "1234" this return byte[]
but when my string is "12345" this failed due to Index and length must refer to a location within the string
Why this is happening ?