/// <summary>
/// ConvertUtil 的摘要说明。
/// </summary>
public class ConvertUtil
{
//将字节数组转化为字符串
public static string Byte2HEX(byte[] ibs)
{
char[] Digit = { ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘ };
char[] ob = new char[2];
StringBuilder returnStr = new StringBuilder();
for (int i = 0; i < ibs.Length; i++)
{
ob[0] = Digit[(ibs[i] >> 4) & 0X0F];
ob[1] = Digit[ibs[i] & 0X0F];
string s = new string(ob);
returnStr.Append(s);
}
return returnStr.ToString();
}
private static int HexChar2Int(char hexChar)
{
switch (hexChar)
{
case ‘0‘:
return 0;
case ‘1‘:
return 1;
case ‘2‘:
return 2;
case ‘3‘:
return 3;
case ‘4‘:
return 4;
case ‘5‘:
return 5;
case ‘6‘:
return 6;
case ‘7‘:
return 7;
case ‘8‘:
return 8;
case ‘9‘:
return 9;
case ‘A‘:
return 10;
case ‘B‘:
return 11;
case ‘C‘:
return 12;
case ‘D‘:
return 13;
case ‘E‘:
return 14;
case ‘F‘:
return 15;
default:
return 0;
}
}
//将字符串转化为字节数组
public static byte[] Hex2Byte(string strHex)
{
byte[] bytes = null;
try
{
char[] charHex = strHex.ToUpper().ToCharArray();
int bytesLength = charHex.Length / 2;
bytes = new byte[bytesLength];
if (bytesLength <= 0)
{
return bytes;
}
int[] tempBytes = new int[2];
//循环每次处理两个字符
for (int i = 0; i < charHex.Length; i=i+2)
{
tempBytes[0] = HexChar2Int(charHex[i]) << 4 & 0XF0;
tempBytes[1] = HexChar2Int(charHex[i + 1]);
bytes[i / 2] = (byte)(tempBytes[0] + tempBytes[1]);
}
}
catch(Exception ex)
{
throw ex;
}
return bytes;
}
/// <summary>
/// 是否是整数
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool IsInt(string str)
{
return Regex.IsMatch(str, "^([0-9]*)$");
}
}