代码:
/// <summary> /// 16进制原码字符串转字节数组 /// </summary> /// <param name="hexString">"AABBCC"或"AA BB CC"格式的字符串</param> /// <returns></returns> public static byte[] ConvertHexStringToBytes(string hexString) { hexString = hexString.Replace(" ", ""); if (hexString.Length % 2 != 0) { throw new ArgumentException("参数长度不正确"); } byte[] returnBytes = new byte[hexString.Length / 2]; for (int i = 0; i < returnBytes.Length; i++) { returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16); } return returnBytes; }