private byte[] StringToByteArray(String str)
{
if ((str.Length % 2) != 0) {
throw new ArgumentException("Invalid string length");
}
byte[] bytes = new byte[str.Length / 2];
for (int i = 0; i<str.Length; i+=2) {
bytes[i/2] = Convert.ToByte (str.Substring(i, 2), 16);
}
return bytes;
}