public static byte[] HexStrToBytes(string hexStr) { hexStr = hexStr.Replace(" ", ""); hexStr = hexStr.Replace("\r", ""); hexStr = hexStr.Replace("\n", ""); hexStr = hexStr.ToUpper(); if (hexStr.Length % 2 != 0) { throw new Exception("Invalid Hex string: odd length."); } for (int index = 0; index < hexStr.Length; ++index) { if (!Asn1Util.IsValidHexDigits(hexStr[index])) { throw new Exception("Invalid Hex string: included invalid character [" + (object)hexStr[index] + "]"); } } int length = hexStr.Length / 2; byte[] numArray = new byte[length]; for (int index = 0; index < length; ++index) { int num = (int)Asn1Util.GetHexDigitsVal(hexStr[index * 2]) << 4 | (int)Asn1Util.GetHexDigitsVal(hexStr[index * 2 + 1]); numArray[index] = (byte)num; } return(numArray); }