/// <summary>
/// 将16进制转换为有符号的10进制
/// </summary>
/// <param name="hexstr"></param>
/// <returns></returns>
public string ConvertHexToSIntStr(string hexstr)
{
if (hexstr.StartsWith("0x"))
{
hexstr = hexstr.Substring(2);
}
//如果不是有效的16进制字符串或者字符串长度大于16或者是空,均返回NULL
if (!IsHexadecimal(hexstr) || hexstr.Length > 16 || string.IsNullOrEmpty(hexstr))
{
return null;
}
if (hexstr.Length > 8)
{
return Convert.ToInt64(hexstr, 16).ToString();
}
else if (hexstr.Length > 4)
{
return Convert.ToInt32(hexstr, 16).ToString();
}
else
{
return Convert.ToInt16(hexstr, 16).ToString();
}
}
/// <summary>
/// 判断是否是十六进制格式字符串
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public bool IsHexadecimal(string str)
{
const string PATTERN = @"[A-Fa-f0-9]+$";
return System.Text.RegularExpressions.Regex.IsMatch(str, PATTERN);
}