当前位置: 首页 > news >正文

CRC8算法DELPHI源码

unit Crc8;

interface

Uses
Classes, Windows;

Function Crc_8n(p : array of BYTE; len : BYTE) : Byte;

implementation

Function Crc_8n(p : array of BYTE; len : BYTE) : Byte;
Var
j, cbit, aout, crc, crc_a, crc_b : Byte;
i : integer;
begin
crc := 0;
i := 0;

// 取移位的位
repeat
crc_a := p[i];
inc(i);
j := 8;
cbit := 1;
repeat
crc_b := crc_a;
crc_b := crc_b xor crc; // ?????
aout := crc_b and cbit;
if aout<>0 then begin
crc := crc xor $18; // ?????
crc := crc shr 1;
crc := crc or $80;
end else begin
crc := crc shr 1;
end;
crc_a := crc_a shr 1;
dec(j);
until j = 0;
dec(len);
until len = 0;

result := crc;
end;

end.


=================================


unit main;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,
Crc8;

type
TForm1 = class(TForm)
Edit1: TEdit;
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

const MinBase = 2; 
MaxBase = 36;

function StrToNum (const s: string; base: Integer; 
neg: Boolean; max: Integer): Integer; 
// s = 要转换的字符串
// base = 进制数
// neg = 是否为负数

// max = 要转换的最大数// 
// 用法: 
// i:= StrToNum (''''''''00101101'''''''', 2, false, MaxInt); 
// i:= StrToNum (''''''''002D'''''''', 16, false, MaxInt); 
// i:= StrToNum (''''''''-45'''''''', 10, true, MaxInt); 
// i:= StrToNum (''''''''ZZ'''''''', 36, true, MaxInt); 
// 
var negate, done: Boolean; 
i, len, digit, mmb: Integer;
c: Char; 
mdb, res: Integer; 
begin 
res:= 0; i:= 1; digit:= 0; 
if (base >= MinBase) and (base <= MaxBase) then begin 
mmb:= max mod base; 
mdb:= max div base; 
len:= Length (s); 
negate:= False; 
while (i <= len) and (s[i] = '''' '''') do Inc (i); 
if neg then begin 
case s[i] of 
'''' '''': Inc (i);
''''-'''': begin Inc (i); negate:= TRUE; end;
end; (* CASE *)
end; (* IF neg *) 
done:= len > i; 
while (i <= len) and done do begin 
c:= Upcase (s[i]); 
case c of 
''''0''''..''''9'''': digit:= ORD(c) - 48;
''''A''''..''''Z'''': digit:= ORD(c) - 55;
else done:= FALSE
end; (* CASE *) 
done:= done and (digit < base); 
if done then begin 
done:= (res < mdb) or ((res = mdb) and (digit <= mmb)); 
IF done then begin 
res:= res * base digit; 
Inc (i); 
end; (* IF done *) 
end; (* IF done *) 
end; (* WHILE *) 
if negate then res:= - res; 
end; (* IF done *) 
Result:= res; 
end;

procedure TForm1.Button1Click(Sender: TObject);
Var
S : String;
P : Array[0..255] of Byte;
Len : Byte;
R : Byte;
I : Integer;
begin
S := Edit1.Text;

if length(s) mod 2 = 1 then s := s ''''0'''';

Memo1.Lines.Add(S '''' :'''');
for i:=1 to length(s) div 2 do begin
p[i-1] := BYTE(StrToNum(copy(s, (i-1)*2 1, 2), 16, false, 500));
Memo1.Lines.Add(IntToStr(I) '''' --> '''' IntToHex(p[i-1], 2));
end;

Len := length(s) div 2;

R := Crc_8n(P, Len);
Memo1.Lines.Add(''''Crc8 Result: '''' IntToHex(R, 2));
end;

end.

转载于:https://www.cnblogs.com/MaxWoods/p/3743981.html

相关文章:

  • NHibernate 快速入门(四)使用 HQL 查询数据
  • HashMap底层实现原理
  • Location Aware DNS Server-----项目部署说明
  • pyqt 调用QT设计师创建的对话框
  • 艾伟_转载:下载文件时根据MIME类型自动判断保存文件的扩展名
  • Oracle学习手札
  • 艾伟_转载:把委托说透(1):开始委托之旅 委托与接口
  • centos7常见问题(更新。。。)
  • 洛谷 P3388 【模板】割点(割顶)题解
  • 大型网站的监控、报警与故障转移
  • mjpg-streamer译文
  • 一起谈.NET技术,.NET Framework源码研究系列之---Delegate
  • gnu下的arm汇编伪指令:.word说明
  • re
  • python循环语句
  • [PHP内核探索]PHP中的哈希表
  • [数据结构]链表的实现在PHP中
  • Apache Zeppelin在Apache Trafodion上的可视化
  • ES6语法详解(一)
  • iOS筛选菜单、分段选择器、导航栏、悬浮窗、转场动画、启动视频等源码
  • java正则表式的使用
  • JS实现简单的MVC模式开发小游戏
  • PHP 程序员也能做的 Java 开发 30分钟使用 netty 轻松打造一个高性能 websocket 服务...
  • Promise面试题,控制异步流程
  • Webpack 4 学习01(基础配置)
  • 表单中readonly的input等标签,禁止光标进入(focus)的几种方式
  • 动态魔术使用DBMS_SQL
  • 复杂数据处理
  • 技术发展面试
  • 简析gRPC client 连接管理
  • 判断客户端类型,Android,iOS,PC
  • 软件开发学习的5大技巧,你知道吗?
  • 一文看透浏览器架构
  • 3月7日云栖精选夜读 | RSA 2019安全大会:企业资产管理成行业新风向标,云上安全占绝对优势 ...
  • 如何在 Intellij IDEA 更高效地将应用部署到容器服务 Kubernetes ...
  • ​ ​Redis(五)主从复制:主从模式介绍、配置、拓扑(一主一从结构、一主多从结构、树形主从结构)、原理(复制过程、​​​​​​​数据同步psync)、总结
  • ​DB-Engines 12月数据库排名: PostgreSQL有望获得「2020年度数据库」荣誉?
  • ​人工智能之父图灵诞辰纪念日,一起来看最受读者欢迎的AI技术好书
  • ​软考-高级-系统架构设计师教程(清华第2版)【第20章 系统架构设计师论文写作要点(P717~728)-思维导图】​
  • ​中南建设2022年半年报“韧”字当头,经营性现金流持续为正​
  • # 安徽锐锋科技IDMS系统简介
  • #NOIP 2014#Day.2 T3 解方程
  • #pragma预处理命令
  • $().each和$.each的区别
  • $HTTP_POST_VARS['']和$_POST['']的区别
  • (Redis使用系列) Springboot 实现Redis 同数据源动态切换db 八
  • (附源码)SSM环卫人员管理平台 计算机毕设36412
  • (附源码)ssm失物招领系统 毕业设计 182317
  • (学习日记)2024.03.12:UCOSIII第十四节:时基列表
  • (原创) cocos2dx使用Curl连接网络(客户端)
  • ./mysql.server: 没有那个文件或目录_Linux下安装MySQL出现“ls: /var/lib/mysql/*.pid: 没有那个文件或目录”...
  • .Net Core/.Net6/.Net8 ,启动配置/Program.cs 配置
  • .Net 访问电子邮箱-LumiSoft.Net,好用
  • .Net的DataSet直接与SQL2005交互
  • .NET国产化改造探索(三)、银河麒麟安装.NET 8环境