16进制字符串转16进制数组16进制字符串转换为byte数组///<summary>
/// 16进制字符转换为byte数组
///</summary>
///<param name="hexString">偶数位,由16进制字符[0-9a-fA-F]组成</param>
///<returns>null为转换失败</returns>
private byte[] HexStringToBytes(string hexString)
{
if (string.IsNullOrEmpty(hexString))
return null;
int hexStringLength = hexString.Length;
if (hexStringLength % 2 != 0)
return null;
Regex r = new Regex(@"[0-9a-fA-F]");
if (!r.IsMatch(hexString))
return null;
int bufferLength = hexStringLength / 2;
byte[] buffer = new byte[bufferLength];
for (int i = 0; i < bufferLength; i++)
{
buffer[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16); //2个16进制字符转换为⼀个byte
}
return buffer;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。