C# Crc算法
来源:互联网 发布:欧陆风云4mac修改时间 编辑:程序博客网 时间:2024/05/17 01:46
public class Crc
{
private static ushort[] Crc16Table = null;
private static uint[] Crc32Table = null;
private static void MakeCRC16Table()
{
if (Crc16Table != null) return;
Crc16Table = new ushort[256];
for (ushort i = 0; i < 256; i++)
{
ushort vCRC = i;
for (int j = 0; j < 8; j++)
if (vCRC % 2 == 0)
vCRC = (ushort)(vCRC >> 1);
else vCRC = (ushort)((vCRC >> 1) ^ 0x8408);
Crc16Table[i] = vCRC;
}
}
private static void MakeCRC32Table()
{
if (Crc32Table != null) return;
Crc32Table = new uint[256];
for (uint i = 0; i < 256; i++)
{
uint vCRC = i;
for (int j = 0; j < 8; j++)
if (vCRC % 2 == 0)
vCRC = (uint)(vCRC >> 1);
else vCRC = (uint)((vCRC >> 1) ^ 0xEDB88320);
Crc32Table[i] = vCRC;
}
}
public static ushort UpdateCRC16(byte bt, ushort seed)
{
return (ushort)(Crc16Table[(seed & 0x000000FF) ^ bt] ^ (seed >> 8));
}
public static uint UpdateCRC32(byte bt, uint seed)
{
return (uint)(Crc32Table[(seed & 0x000000FF) ^ bt] ^ (seed >> 8));
}
public static ushort CRC16(byte[] bts)
{
MakeCRC16Table();
ushort Result = 0xFFFF;
foreach (byte vByte in bts)
Result = UpdateCRC16(vByte, Result);
return (ushort)(~Result);
}
public static ushort CRC16(string str, Encoding encoding)
{
return CRC16(encoding.GetBytes(str));
}
public static ushort CRC16(string str)
{
return CRC16(str, Encoding.UTF8);
}
public static uint CRC32(byte[] bts)
{
MakeCRC32Table();
uint result = 0xFFFFFFFF;
foreach (byte vByte in bts)
result = UpdateCRC32(vByte, result);
return (uint)(~result);
}
public static uint CRC32(string str, Encoding encoding)
{
return CRC32(encoding.GetBytes(str));
}
public static uint CRC32(string str)
{
return CRC32(str, Encoding.Unicode);
}
}
{
private static ushort[] Crc16Table = null;
private static uint[] Crc32Table = null;
private static void MakeCRC16Table()
{
if (Crc16Table != null) return;
Crc16Table = new ushort[256];
for (ushort i = 0; i < 256; i++)
{
ushort vCRC = i;
for (int j = 0; j < 8; j++)
if (vCRC % 2 == 0)
vCRC = (ushort)(vCRC >> 1);
else vCRC = (ushort)((vCRC >> 1) ^ 0x8408);
Crc16Table[i] = vCRC;
}
}
private static void MakeCRC32Table()
{
if (Crc32Table != null) return;
Crc32Table = new uint[256];
for (uint i = 0; i < 256; i++)
{
uint vCRC = i;
for (int j = 0; j < 8; j++)
if (vCRC % 2 == 0)
vCRC = (uint)(vCRC >> 1);
else vCRC = (uint)((vCRC >> 1) ^ 0xEDB88320);
Crc32Table[i] = vCRC;
}
}
public static ushort UpdateCRC16(byte bt, ushort seed)
{
return (ushort)(Crc16Table[(seed & 0x000000FF) ^ bt] ^ (seed >> 8));
}
public static uint UpdateCRC32(byte bt, uint seed)
{
return (uint)(Crc32Table[(seed & 0x000000FF) ^ bt] ^ (seed >> 8));
}
public static ushort CRC16(byte[] bts)
{
MakeCRC16Table();
ushort Result = 0xFFFF;
foreach (byte vByte in bts)
Result = UpdateCRC16(vByte, Result);
return (ushort)(~Result);
}
public static ushort CRC16(string str, Encoding encoding)
{
return CRC16(encoding.GetBytes(str));
}
public static ushort CRC16(string str)
{
return CRC16(str, Encoding.UTF8);
}
public static uint CRC32(byte[] bts)
{
MakeCRC32Table();
uint result = 0xFFFFFFFF;
foreach (byte vByte in bts)
result = UpdateCRC32(vByte, result);
return (uint)(~result);
}
public static uint CRC32(string str, Encoding encoding)
{
return CRC32(encoding.GetBytes(str));
}
public static uint CRC32(string str)
{
return CRC32(str, Encoding.Unicode);
}
}
0 0
- CRC算法C#实现
- CRC算法C#实现
- C# Crc算法
- C#实现CRC校验算法
- CRC校验算法及C#程序实现
- CRC校验算法及C#程序实现
- CRC校验算法及C#程序实现
- CRC算法
- CRC算法
- CRC算法
- CRC算法
- crc 算法
- CRC算法
- CRC算法
- CRC算法
- CRC算法
- CRC算法
- CRC算法
- mysql语句优化
- 1 课务 IOS概述
- Almost done.
- leetcode-4sum
- WINDOWS API串口编程
- C# Crc算法
- Java笔记(2)
- spring 3.x企业应用 --读后感和体会2
- 交通灯管理系统
- P89LPC925单片机串口封装
- 加油!
- 在头文件中使用static定义变量意味着什么
- 标准SQL语句(5) --- 表的查询(3)
- 做好自己