1、把string 类型修改成array of byte是一样的,你可以看函数体
2、有个函数可以把byte的数组转成字符串数组,但是我没有测试过,建议用第一中方法
function ByteToStr(T: array of byte): string;
const
Digits: array[0..15] of char =
('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
var
I: integer;
begin
Result := '';
for I := Low(T) to High(T) do
Result := Result + Digits[(T shr 4) and $0f] + Digits[T and $0f];
end;
另外你可以把正确的结果发出来,大家帮你调一下,这个代码也是网上的
下面是我自己设置的一些代码,两种方式看看哪钟能满足你的要求
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, OleServer, Word2000, OleCtnrs;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
function CalcCRC(s : array of byte): Char;overload;
function CalcCRC(s : string): Char;overload;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
FBuf:array[1..5] of byte;
strCRC: String;
strTemp : string;
function ByteToStr(T: array of byte): string;
const
Digits: array[0..15] of char =
('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
var
I: integer;
begin
Result := '';
for I := Low(T) to High(T) do
Result := Result + Digits[(T shr 4) and $0f] + Digits[T and $0f];
end;
begin
FBuf[1]:=$43;
FBuf[2]:=$23;
FBuf[3]:=$56;
FBuf[4]:=$e4;
FBuf[5]:=$01;
strCRC := CalcCRC(fBuf);// 用array of byte的方式
strTemp:= ByteToStr(fBuf);// 用string的方式
strCRC := CalcCRC(strTemp);
end;
Function TForm1.CalcCRC(s : array of byte):Char;
Var
q,r,Crc:word;
B:byte;
begin
Crc:=Byte(S[1]);
B:=Byte(S[2]);
R:=0;
Q:=1;
CRC:=Crc shl 1;
while Q<6 do begin
if ((b and ($80 shr r))<>0) then CRC:=CRC or 1;
if CRC>=$100 then CRC:=CRC xor $107;
CRC:=CRC shl 1;
Inc(R);
if r=8 then begin
r:=0;
inc(q);
b:=Byte(S[Q+1]);
end;
end;
B:=CRC shr 1;
B:=not B;
result:=chr(B);
end;
Function TForm1.CalcCRC(s : string):Char;
Var
q,r,Crc:word;
B:byte;
begin
Crc:=Byte(S[1]);
B:=Byte(S[2]);
R:=0;
Q:=1;
CRC:=Crc shl 1;
while Q<6 do begin
if ((b and ($80 shr r))<>0) then CRC:=CRC or 1;
if CRC>=$100 then CRC:=CRC xor $107;
CRC:=CRC shl 1;
Inc(R);
if r=8 then begin
r:=0;
inc(q);
b:=Byte(S[Q+1]);
end;
end;
B:=CRC shr 1;
B:=not B;
result:=chr(B);
end;
end.