public static byte[] HexToByte(string msg)
{
if(String.IsNullOrEmpty(msg))
return new byte[0];
//remove any spaces from the string
msg = msg.Replace(" ", "");
//create a byte array the length of the
//divided by 2 (Hex is 2 characters in length)
var comBuffer = new byte[msg.Length / 2];
//loop through the length of the provided string
for (int i = 0; i < msg.Length; i += 2)
//convert each set of 2 characters to a byte
//and add to the array
comBuffer[i / 2] = Convert.ToByte(msg.Substring(i, 2), 16);
//return the array
return comBuffer;
}