110,536
社区成员
发帖
与我相关
我的任务
分享
function mjComCRC(buf: PByte;len: Integer): Word;
var
i,j: Integer;
chr: Word;
begin
Result := $ffff;
for j:=0 to len-1 do begin
chr := PByte(LongWord(buf)+j)^ and $ff;
Result := Result xor chr;
for i:=0 to 8 -1 do begin
if ((Result and 01)<>0) then
Result := (Result shr 1) xor $a001
else
Result := Result shr 1;
end;
Result := Result and $ffff;
end;
end;
function crc16(str11:string):string;
var
str,Str1,str2: String;
Ret: Word;
mybyte:pchar;
i: integer;
begin
Str1 := str11; //假定你用Edit1输入
SetLength(Str,Length(Str1));
i := HexToBin(PChar(str1),PChar(Str),Length(Str1));
SetLength(Str,i);
Ret := mjComCRC(pbyte(Str),i);
GetMem(mybyte,sizeof(Ret) * 2);
BinToHex(PChar(@Ret),PChar(mybyte),sizeof(ret));
str2:=copy(strpas(mybyte),1,4);
FreeMem(mybyte,sizeof(Ret) * 2);
result:= copy(str2,1,2)+copy(str2,3,2);
end;
function HexStrToStr(const S:string):string;
//16进制字符串转换成字符串
var
t:Integer;
ts:string;
M,Code:Integer;
begin
t:=1;
Result:='';
while t<=Length(S) do
begin //xlh 2006.10.21
while (t<=Length(S)) and (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 strtohexstr(const S:string):string;
//字符串转换成16进制字符串
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;