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.

CRC8算法DELPHI源码

时间: 2024-08-02 15:12:04

CRC8算法DELPHI源码的相关文章

简易文本编辑器(Delphi源码)

计划一个简略单纯的文本编纂器,具有建立.编纂.生存平凡文本文件的功能. unit U nit1; interface uses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  Dialogs, StdCtrls, ExtCtrls; type  TForm1 = class(TForm)    Panel1: TPanel;    Memo1: TMemo;    OpenDialog1:

SURF算法与源码分析、下

上一篇文章 SURF算法与源码分析.上 中主要分析的是SURF特征点定位的算法原理与相关OpenCV中的源码分析,这篇文章接着上篇文章对已经定位到的SURF特征点进行特征描述.这一步至关重要,这是SURF特征点匹配的基础.总体来说算法思路和SIFT相似,只是每一步都做了不同程度的近似与简化,提高了效率. 1. SURF特征点方向分配 为了保证特征矢量具有旋转不变性,与SIFT特征一样,需要对每个特征点分配一个主方向.为些,我们需要以特征点为中心,以$6s$($s = 1.2 *L /9$为特征点

Chrom浏览器开发DELPHI源码谷歌内核简装版本界面需要自己搞

演示地址:链接: https://pan.baidu.com/s/1vfHhslV5breCankc3s1lOQ 密码: 9njm Delphi源码,付款后自动网盘链接 : QQ群:621816328 原文地址:https://www.cnblogs.com/plug/p/8665212.html

Spark MLlib机器学习算法、源码及实战讲解pdf电子版下载

Spark MLlib机器学习算法.源码及实战讲解pdf电子版下载 链接:https://pan.baidu.com/s/1ruX9inG5ttOe_5lhpK_LQg 提取码:idcb <Spark MLlib机器学习:算法.源码及实战详解>书中讲解由浅入深慢慢深入,解析讲解了MLlib的底层原理:数据操作及矩阵向量计算操作,该部分是MLlib实现的基础:并对此延伸机器学习的算法,循序渐进的讲解其中的原理,是读者一点一点的理解和掌握书中的知识. 目录 · · · · · · 第一部分 Spa

Weka算法Classifier-meta-AdditiveRegression源码分析

博主最近迷上了打怪物猎人,这片文章拖了很久才开始动笔 一.算法 AdditiveRegression,换个更出名一点的叫法可以称作GBDT(Grandient Boosting Decision Tree)梯度下降分类树,或者GBRT(Grandient Boosting Regression Tree)梯度下降回归树,是一种多分类器组合的算法,更确切的说,是属于Boosting算法. 谈到Boosting算法,就不能不提AdaBoost,参见之前我写的博客,可以看到AdaBoost的核心是级联

Weka算法Clusterers-Xmeans源码分析

</pre><p></p><p><span style="font-size:18px">上几篇博客都是分析的分类器算法(有监督学习),这次就分析一个聚类算法(无监督学习).</span></p><p><span style="font-size:18px"></span></p><p><span style=&quo

Weka算法Classifier-trees-REPTree源码分析(一)

一.算法 关于REPTree我实在是没找到什么相关其算法的资料,或许是Weka自创的一个关于决策树的改进,也许是其它某种决策树方法的别名,根据类的注释:Fast decision tree learner. Builds a decision/regression tree using information gain/variance and prunes it using reduced-error pruning (with backfitting).  Only sorts values

Expectation Maximization-EM(期望最大化)-算法以及源码

在统计计算中,最大期望(EM)算法是在概率(probabilistic)模型中寻找参数最大似然估计的算法,其中概率模型依赖于无法观测的隐藏变量(Latent Variable).最大期望经常用在机器学习和计算机视觉的数据聚类(Data Clustering) 领域.最大期望算法经过两个步骤交替进行计算,第一步是计算期望(E),利用对隐藏变量的现有估计值,计算其最大似然估计值:第二步是最大化(M),最大 化在 E 步上求得的最大似然值来计算参数的值.M 步上找到的参数估计值被用于下一个 E 步计算

SURF算法与源码分析、上

如果说SIFT算法中使用DOG对LOG进行了简化,提高了搜索特征点的速度,那么SURF算法则是对DoH的简化与近似.虽然SIFT算法已经被认为是最有效的,也是最常用的特征点提取的算法,但如果不借助于硬件的加速和专用图像处理器的配合,SIFT算法以现有的计算机仍然很难达到实时的程度.对于需要实时运算的场合,如基于特征点匹配的实时目标跟踪系统,每秒要处理8-24帧的图像,需要在毫秒级内完成特征点的搜索.特征矢量生成.特征矢量匹配.目标锁定等工作,这样SIFT算法就很难适应这种需求了.SURF借鉴了S