delphi 异或校验的问题

petsoft 2016-12-18 11:08:25
校验合,有的要加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;

...全文
550 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧