庆祝开通blog,同时记下C#中如何进行CRC校验与浮点数的转换
来源:互联网 发布:sql语句修改属性 编辑:程序博客网 时间:2024/05/29 19:37
源于CSDN
/// <summary>
/// Crc16
/// </summary>
/// <param name="buffer"></param>
public void Crc(byte[] buffer)
{
Crc(buffer,0,buffer.Length);
}
CRC 校验算法C#实现一 ——接口实现
CRC 校验有 CRC 8位 16位 32位所以在实现这些校验算发之前,先实现它们的接口!
using System;
using System;
namespace CRC
{
public interface ICRC
{
long Value
{
get;
}
void Reset();
void Crc(int bval);
void Crc(byte[] buffer);
void Crc(byte[] buf, int off, int len);
{
public interface ICRC
{
long Value
{
get;
}
void Reset();
void Crc(int bval);
void Crc(byte[] buffer);
void Crc(byte[] buf, int off, int len);
}
}
}
CRC 校验算法C#实现三 ——16位校验算法
using System;
namespace CRC
{
/// <summary>
/// CRC16 的摘要说明。
/// </summary>
public class CRC16:ICRC
{
#region CRC 16 位校验表
/// <summary>
/// 16 位校验表 Upper 表
/// </summary>
public ushort[] uppercrctab = new ushort[]
{
0x0000,0x1231,0x2462,0x3653,0x48c4,0x5af5,0x6ca6,0x7e97,
0x9188,0x83b9,0xb5ea,0xa7db,0xd94c,0xcb7d,0xfd2e,0xef1f
};
{
/// <summary>
/// CRC16 的摘要说明。
/// </summary>
public class CRC16:ICRC
{
#region CRC 16 位校验表
/// <summary>
/// 16 位校验表 Upper 表
/// </summary>
public ushort[] uppercrctab = new ushort[]
{
0x0000,0x1231,0x2462,0x3653,0x48c4,0x5af5,0x6ca6,0x7e97,
0x9188,0x83b9,0xb5ea,0xa7db,0xd94c,0xcb7d,0xfd2e,0xef1f
};
/// <summary>
/// 16 位校验表 Lower 表
/// </summary>
public ushort[] lowercrctab = new ushort[]
{
0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7,
0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef
};
#endregion
/// 16 位校验表 Lower 表
/// </summary>
public ushort[] lowercrctab = new ushort[]
{
0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7,
0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef
};
#endregion
ushort crc = 0;
/// <summary>
/// 校验后的结果
/// </summary>
public long Value
{
get
{
return crc;
}
set
{
crc = (ushort)value;
}
}
/// 校验后的结果
/// </summary>
public long Value
{
get
{
return crc;
}
set
{
crc = (ushort)value;
}
}
/// <summary>
/// 设置crc 初始值
/// </summary>
public void Reset()
{
crc = 0;
}
/// <summary>
/// Crc16
/// </summary>
/// <param name="ucrc"></param>
/// <param name="buf"></param>
public void Crc(ushort ucrc,byte[] buf)
{
crc = ucrc;
Crc(buf);
}
/// <summary>
/// Crc16
/// </summary>
/// <param name="bval"></param>
public void Crc(int bval)
{
ushort h = (ushort)((crc >> 12) & 0x0f);
ushort l = (ushort)((crc >> 8 ) & 0x0f);
ushort temp = crc;
temp =(ushort)(((temp & 0x00ff) << 8) | bval);
temp =(ushort)(temp ^(uppercrctab[(h -1) + 1] ^ lowercrctab[(l - 1) + 1]));
crc = temp;
}
/// 设置crc 初始值
/// </summary>
public void Reset()
{
crc = 0;
}
/// <summary>
/// Crc16
/// </summary>
/// <param name="ucrc"></param>
/// <param name="buf"></param>
public void Crc(ushort ucrc,byte[] buf)
{
crc = ucrc;
Crc(buf);
}
/// <summary>
/// Crc16
/// </summary>
/// <param name="bval"></param>
public void Crc(int bval)
{
ushort h = (ushort)((crc >> 12) & 0x0f);
ushort l = (ushort)((crc >> 8 ) & 0x0f);
ushort temp = crc;
temp =(ushort)(((temp & 0x00ff) << 8) | bval);
temp =(ushort)(temp ^(uppercrctab[(h -1) + 1] ^ lowercrctab[(l - 1) + 1]));
crc = temp;
}
/// <summary>
/// Crc16
/// </summary>
/// <param name="buffer"></param>
public void Crc(byte[] buffer)
{
Crc(buffer,0,buffer.Length);
}
/// <summary>
/// Crc16
/// </summary>
/// <param name="buf"></param>
/// <param name="off"></param>
/// <param name="len"></param>
public void Crc(byte[] buf,int off,int len)
{
if (buf == null)
{
throw new ArgumentNullException("buf");
}
if (off < 0 || len < 0 || off + len > buf.Length)
{
throw new ArgumentOutOfRangeException();
}
for (int i = off; i < len ; i ++)
{
Crc(buf[i]);
}
}
/// Crc16
/// </summary>
/// <param name="buf"></param>
/// <param name="off"></param>
/// <param name="len"></param>
public void Crc(byte[] buf,int off,int len)
{
if (buf == null)
{
throw new ArgumentNullException("buf");
}
if (off < 0 || len < 0 || off + len > buf.Length)
{
throw new ArgumentOutOfRangeException();
}
for (int i = off; i < len ; i ++)
{
Crc(buf[i]);
}
}
}
}
}
调用CRC16 的一个例子
CRC16 crc = new CRC16();
crc.Reset();
crc.Crc(buf,0,DataPack.NextPackDataByteCount - 2);
crc.Crc(0);
crc.Crc(0);
buf[DataPack.NextPackDataByteCount - 2] = (byte)((crc.Value >> 8) & 0xff);
buf[DataPack.NextPackDataByteCount - 1] = (byte)((crc.Value & 0xff));
crc.Reset();
crc.Crc(buf,0,DataPack.NextPackDataByteCount - 2);
crc.Crc(0);
crc.Crc(0);
buf[DataPack.NextPackDataByteCount - 2] = (byte)((crc.Value >> 8) & 0xff);
buf[DataPack.NextPackDataByteCount - 1] = (byte)((crc.Value & 0xff));
FLOAT_HEX HEX_FLOAT
float vDouble =77.79F;
Text = BitConverter.ToString(BitConverter.GetBytes(vDouble)).Replace("-", " ");
string m=Text.Replace(" ", "");//如果中间有空格
int len = m.Length/2;
byte[] arr = new byte[len];
for(int i=0;i<len;i++)
{
arr[i] = Convert.ToByte(m.Substring(i*2,2),16);
}
float f1 = BitConverter.ToSingle(arr,0);
- 庆祝开通blog,同时记下C#中如何进行CRC校验与浮点数的转换
- 如何进行CRC校验
- 如何进行CRC校验
- 庆祝我的blog开通
- 庆祝我的blog开通
- 庆祝自己的Blog开通
- 庆祝Stonee的blog开通
- 庆祝自己的 BLOG 开通!
- Blog 开通!~ 庆祝庆祝 ~
- 第一帖。庆祝csdn的blog开通。
- 庆祝我的blog正式开通!!!
- 我的CSDN BLOG开通, 庆祝一下.
- 自我庆祝自己的Blog开通
- 庆祝我的blog正式开通
- 自己庆祝自己的blog开通
- 庆祝blog开通!
- 热烈庆祝Blog开通
- blog开通,庆祝一下
- SQL高手篇
- MyEclipse 5.1.0GA_E3.2.1版破解
- 如何从URL取得其在Internet Cache中的文件名
- 天使的眼泪
- 对象内存池技术
- 庆祝开通blog,同时记下C#中如何进行CRC校验与浮点数的转换
- SQL语法大全
- delphi 调用 Excel 宏
- 测试管理过程
- 我的2006
- 有一种美好能感动一生
- 在北大校园BBS引起轰动的一篇文章
- 中国饭局里的潜规则
- 找个“胃趣”相投的老婆