栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

CRC16CCITT算法CcittKermit

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

CRC16CCITT算法CcittKermit

常见CRC算法模型如下:

CRC校验工具-在线工具

1)python

binascii.crc_hqx
crcmod.mkCrcFun

​​​​​​Python binascii.crc_hqx方法代碼示例 - 純淨天空

​​​​​​Python Examples of crcmod.mkCrcFun

crcmod – CRC calculation — crcmod v1.7 documentation

2)c#版本

public enum Crc16Mode : ushort { Standard = 0xA001, CcittKermit = 0x8408 }

public class Crc16
{
	readonly ushort[] table = new ushort[256];

	public ushort ComputeChecksum(params byte[] bytes)
	{
		ushort crc = 0;
		for (int i = 0; i < bytes.Length; ++i)
		{
			byte index = (byte)(crc ^ bytes[i]);
			crc = (ushort)((crc >> 8) ^ table[index]);
		}
		return crc;
	}
	
	public ushort ComputeChecksumCanny(params byte[] bytes)
	{
		ushort crc = 0;
		for (int i = 0; i < bytes.Length; ++i)
		{
			byte index = (byte)((crc>>8) ^ bytes[i]);
			crc = (ushort)((crc << 8) ^ table[index]);
		}
		return crc;
	}
	
	public ushort ComputeChecksumCanny2(ushort crcInit,params byte[] bytes)
	{
		ushort crc = crcInit;
		for (int i = 0; i < bytes.Length; ++i)
		{
			byte index = (byte)((crc>>8) ^ bytes[i]);
			crc = (ushort)((crc << 8) ^ table[index]);
		}
		return crc;
	}

	public byte[] ComputeChecksumBytes(params byte[] bytes)
	{
		ushort crc = ComputeChecksum(bytes);
		return BitConverter.GetBytes(crc);
	}

	public Crc16(Crc16Mode mode)
	{
		ushort polynomial = (ushort)mode;
		ushort value;
		ushort temp;
		for (ushort i = 0; i < table.Length; ++i)
		{
			value = 0;
			temp = i;
			for (byte j = 0; j < 8; ++j)
			{
				if (((value ^ temp) & 0x0001) != 0)
				{
					value = (ushort)((value >> 1) ^ polynomial);
				}
				else
				{
					value >>= 1;
				}
				temp >>= 1;
			}
			table[i] = value;
			
			//Console.WriteLine($"0x{table[i]:x2}");
		}
	}
}


var crc16 = new Crc16(Crc16Mode.CcittKermit);

var fileWithLocation = "D:\21100815.165";

byte[] bytes = File.ReadAllBytes(fileWithLocation);
Console.WriteLine($"file size:{bytes.Length} bytes");

var crc16Ret = crc16.ComputeChecksum(bytes);
var aa = crc16Ret;
Console.WriteLine($"org CRC16:0x{aa:x2}");

crc16Ret = crc16.ComputeChecksumCanny(bytes);
aa = crc16Ret;
Console.WriteLine($"Canny CRC16:0x{aa:x2}");

A Standard CRC-16 and CRC-16 Kermit implementation in C# - Sanity Free Coding - C#, .NET, PHP

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/307492.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号