完整請參考這篇( https://www.cnblogs.com/ZaraNet/p/11550673.html ),這只是試著寫看看而已
public bool HexTobyte_bool_byte(string input_hex1 ,ref byte value_byte)
{
string PATTERN = @"([^A-Fa-f0-9]|\s+?)+";
char[] Tmp_CharArray1 = input_hex1.ToCharArray();
string input_hex2 = input_hex1;
if (Tmp_CharArray1.Length > 2)
{
if (((Tmp_CharArray1[0] == '0') && (Tmp_CharArray1[1] == 'x')))
{
input_hex2 = "";
for (int i = 2; i < Tmp_CharArray1.Length; i++)
input_hex2 = input_hex2 + Tmp_CharArray1[i];
}
else if (((Tmp_CharArray1[0] == '0') && (Tmp_CharArray1[1] == 'X')))
{
input_hex2 = "";
for (int i = 2; i < Tmp_CharArray1.Length; i++)
input_hex2 = input_hex2 + Tmp_CharArray1[i];
}
}
bool ans = (!System.Text.RegularExpressions.Regex.IsMatch(input_hex2, PATTERN));
if(ans)
value_byte = HexTobyte_byte(input_hex2);
else
value_byte = 0;
return ans;
}