/// <summary>
/// 获取校验码
/// </summary>
/// <param name="bytes">字节数组</param>
/// <param name="start">开始位置,包括此位置</param>
/// <param name="end">结束位置,包括此位置</param>
/// <returns>返回校验码</returns>
public static byte[] GetCrc(byte[] bytes)
{
int crc = 0;
for (int i = 3; i <= bytes.Length - 2; i++)
{
crc = crc + ((int)bytes[i] & 0x00FF);
}
crc = (~crc) + 1;
byte[] result = new byte[2];
result[1] = (byte)(crc & 0x00FF);
result[0] = (byte)((crc >> 8) & 0x00FF);
return result;
}
/// <summary>
/// 字节数组转16进制字符串
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static string byteToHexStr(byte[] bytes)
{
string returnStr = "";
if (bytes != null)
{
for (int i = 0; i < bytes.Length; i++)
{
returnStr += bytes[i].ToString("X2");
}
}
return returnStr;
}
/// <summary>
/// 字符串转十六进制
/// </summary>
/// <param name="hexString"></param>
/// <returns></returns>
public static byte[] StrToHexByte(string hexString)
{
hexString = hexString.Replace(" ", "");//删除空格
if ((hexString.Length % 2) != 0) hexString += " ";
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2).Replace(" ", ""), 16);
return returnBytes;
}
C#中实现16进制获取校验码,进制之间转换
最新推荐文章于 2023-11-28 17:47:25 发布