delphi 异或校验的问题
校验合,有的要加80才对,有的不用加,不知道是什么问题? 有没有懂的老师给指导一下?
function StrToHexStr(const S:string):string;
var
I:Integer;
begin
for I:=1 to Length(S) do
begin
if I=1 then
Result:=' ' + IntToHex(Ord(S[1]),2)+ ' '
else Result:=Result+' '+IntToHex(Ord(S[I]),2);
end;
end;
function HexStrToStr(const S:string):string;
var
t:Integer;
ts:string;
M,Code:Integer;
begin
t:=1;
Result:='';
while t<=Length(S) do
begin
while not (S[t] in ['0'..'9','A'..'F','a'..'f']) do
inc(t);
if (t+1>Length(S))or(not (S[t+1] in ['0'..'9','A'..'F','a'..'f'])) then
ts:='$'+S[t]
else
ts:='$'+S[t]+S[t+1];
Val(ts,M,Code);
if Code=0 then
Result:=Result+Chr(M);
inc(t,2);
end;
end;
function HexToInt(str:string) : word;
var
i , value : word;
pos : word;
begin
value:=0;
pos:=length(str);
for i:=1 to pos do
begin
case str[i] of
'f','F':
value := value*16+15;
'e','E':
value := value*16+14;
'd','D':
value := value*16+13;
'c','C':
value := value*16+12;
'b','B':
value := value*16+11;
'a','A':
value := value*16+10;
'0'..'9':
value := value*16+ord(str[i])-ord('0');
else
result:=value;
exit;
end;
result:=value;
end;
end;
function HexToInt2(Str1: string): longInt;
var i: integer;
begin
Result := 0;
for i := 1 to length(Str1) do
begin
if (Str1[i] >= '0') and (Str1[i] <= '9') then
Result := Result * 16 + StrToInt(Str1[i])
else
begin
if (Str1[i] >= 'A') and (Str1[i] <= 'F') then
result := Result * 16 + ord(Str1[i]) - 55
else
begin
result := 0;
exit;
end;
end;
end;
end;
function GetCheckBcc(AStr: string): byte; //与或计算 Check Xor (BCC)
var
t,i,j: integer;
strNo:string;
A1:Byte;
AChar:Char;
begin
t := 0;
j := (length(AStr) div 3) ;
if (length(AStr) mod 3)>0 then
j:= j+1;
for i:=1 to j do
begin
strNo:=Copy(AStr,(i-1)*3+1,2); // s[i]+s[i+1];
strNo :=HexStrToStr(strNo); //Hex -> Str
AChar :=strNo[1];
t := t xor Ord(Byte(AChar));
end;
Result := t and $7f;
end;

function StrToHexStr(const S:string):string;
var
I:Integer;
begin
for I:=1 to Length(S) do
begin
if I=1 then
Result:=' ' + IntToHex(Ord(S[1]),2)+ ' '
else Result:=Result+' '+IntToHex(Ord(S[I]),2);
end;
end;
function HexStrToStr(const S:string):string;
var
t:Integer;
ts:string;
M,Code:Integer;
begin
t:=1;
Result:='';
while t<=Length(S) do
begin
while not (S[t] in ['0'..'9','A'..'F','a'..'f']) do
inc(t);
if (t+1>Length(S))or(not (S[t+1] in ['0'..'9','A'..'F','a'..'f'])) then
ts:='$'+S[t]
else
ts:='$'+S[t]+S[t+1];
Val(ts,M,Code);
if Code=0 then
Result:=Result+Chr(M);
inc(t,2);
end;
end;
function HexToInt(str:string) : word;
var
i , value : word;
pos : word;
begin
value:=0;
pos:=length(str);
for i:=1 to pos do
begin
case str[i] of
'f','F':
value := value*16+15;
'e','E':
value := value*16+14;
'd','D':
value := value*16+13;
'c','C':
value := value*16+12;
'b','B':
value := value*16+11;
'a','A':
value := value*16+10;
'0'..'9':
value := value*16+ord(str[i])-ord('0');
else
result:=value;
exit;
end;
result:=value;
end;
end;
function HexToInt2(Str1: string): longInt;
var i: integer;
begin
Result := 0;
for i := 1 to length(Str1) do
begin
if (Str1[i] >= '0') and (Str1[i] <= '9') then
Result := Result * 16 + StrToInt(Str1[i])
else
begin
if (Str1[i] >= 'A') and (Str1[i] <= 'F') then
result := Result * 16 + ord(Str1[i]) - 55
else
begin
result := 0;
exit;
end;
end;
end;
end;
function GetCheckBcc(AStr: string): byte; //与或计算 Check Xor (BCC)
var
t,i,j: integer;
strNo:string;
A1:Byte;
AChar:Char;
begin
t := 0;
j := (length(AStr) div 3) ;
if (length(AStr) mod 3)>0 then
j:= j+1;
for i:=1 to j do
begin
strNo:=Copy(AStr,(i-1)*3+1,2); // s[i]+s[i+1];
strNo :=HexStrToStr(strNo); //Hex -> Str
AChar :=strNo[1];
t := t xor Ord(Byte(AChar));
end;
Result := t and $7f;
end;

...全文
相关推荐
对于16进制数异或程序
在编程领域,16进制数的异或操作是一种常见的数据处理方式,特别是在加密、校验和计算以及数据比较中。Delphi作为一种流行的面向对象的编程语言,提供了丰富的功能来支持这种运算。
CRC校验技术与Delphi实现教程
在数学中,多项式是包含变量和系数的表达式,这些变量具有非负整数指数。多项式可以用来表示计算机中的二进制数,比如一个二进制数1011可以表示为多项式。多项式除法是一种模拟长除法过程的算术运算,被广泛应用于CRC校验算法中。多项式除法有几个关键性质:- 多项式的加法与减法运算遵循模2运算规则,即二进制加法,其中1+1=0和1+0=1+0=1。- 多项式的乘法和除法需要模2运算,当涉及到模2除法时,我们使用多项式模除法。本章节对CRC16的生成原理及其计算过程进行了深入的探讨。
用Delphi写的 Modbus 协议的CRC校验源码
通过理解CRC的工作原理和Delphi中的实现方式,可以有效地调试和优化代码,确保数据传输的准确性和可靠性。在处理工业自动化设备之间的通信问题时,这样的源码是不可或缺的资源。
Delphi CRC加密文件算法.rar
**CRC计算原理**:CRC是通过计算数据序列的多项式除法来得到一个校验码。数据被视为二进制多项式,与预定义的生成多项式进行异或运算,最终得到的余数即为CRC校验码。
Crc效验crc.RAR_PB C_crc_crc pb_delphi crc_pb
**进行位移和异或操作**:对每个数据位执行位移操作,然后用CRC多项式异或当前的CRC寄存器值。5. **计算余数**:处理完所有数据位后,CRC寄存器的值就是CRC校验码。6.


语言基础/算法/系统设计
