不好意思各位,上面没有说清楚,strHex它本身就是一个字符串
public static byte[] HexToBytes(string strHex) {
byte[] strBytes=Encoding.ASCII.GetBytes(strHex.ToUpper());

int nCount=strBytes.Length/2;
int i;
for(i=0;i<nCount;i++){
byte tpVal;
if ( strBytes[2 * i] >=65 && strBytes[2 * i] <=70 ) {
tpVal = (byte)((strBytes[2 * i] - 65 + 10) * 16);
} else if( strBytes[2 * i] >= 48 && strBytes[2 * i] <= 57 ){
tpVal = (byte)((strBytes[2 * i] - 48 ) * 16);
}else{
break;
}
if ( strBytes[2 * i+1] >=65 && strBytes[2 * i+1] <=70 ) {
tpVal += (byte)(strBytes[2 * i + 1] - 65 + 10);
} else if( strBytes[2 * i+1] >= 48 && strBytes[2 * i+1] <= 57 ){
tpVal += (byte)(strBytes[2 * i+1] - 48);
}else{
break;
}
strBytes[i]=tpVal;//缓存在strBytes中
}
if (i == 0) {
return null;
} else {
byte[] arrRet=new byte[i];
Array.Copy(strBytes,0,arrRet,0,i);
return arrRet;
}
}