请把下段C++代码改成Delphi。。谢谢 5
unsignedintcal_crc(unsignedcHar*ptr,unsignedcharlen){unsignedchari;unsignedintcrc_val...
unsigned int cal_crc(unsigned cHar *ptr, unsigned char len)
{
unsigned char i;
unsigned int crc_value =0;
while(len--)
{
for(i=0x80; i!=0; i>>=1 )
{
if (crc_value&0x8000)
crc_value = (crc_value << 1) ^0x8005 ;
else
crc_value = crc_value << 1 ;
if(*ptr&i)
crc_value^=0x8005 ;
}
ptr++;
}
return(crc_value);
} 展开
{
unsigned char i;
unsigned int crc_value =0;
while(len--)
{
for(i=0x80; i!=0; i>>=1 )
{
if (crc_value&0x8000)
crc_value = (crc_value << 1) ^0x8005 ;
else
crc_value = crc_value << 1 ;
if(*ptr&i)
crc_value^=0x8005 ;
}
ptr++;
}
return(crc_value);
} 展开
1个回答
展开全部
第一种
function cal_crc( ptr : PByte; len : Byte ) : Cardinal;
var
i : Byte;
begin
Result := 0;
while len > 0 do
begin
i := $80;
while i <> 0 do
begin
if Result and $8000 <> 0 then
Result := ( Result shl 1 ) xor $8005
else
Result := Result shl 1;
if ptr^ and i <> 0 then
Result := Result xor $8005;
i := i shr 1;
end;
Inc( ptr );
Dec( len );
end;
end;
第二种
function cal_crc( ptr : PByte; len : Byte ) : Cardinal;
var
i, J, B : Byte;
begin
Result := 0;
for i := 1 to len do
begin
for J := 7 downto 0 do
begin
B := 1 shl J;
if Result and $8000 <> 0 then
Result := ( Result shl 1 ) xor $8005
else
Result := Result shl 1;
if ptr^ and B <> 0 then
Result := Result xor $8005;
end;
Inc( ptr );
end;
end;
第三种
type
TBitMask = 0..7;
TBitMaskSet = set of TBitMask;
function cal_crc( ptr : PByte; len : Byte ) : Cardinal;
const
CN_FACTOR_AND = $8000;
CN_FACTOR_XOR = $8005;
var
i, J : Byte;
B : Boolean;
begin
Result := 0;
for i := 1 to len do
begin
for J := 7 downto 0 do
begin
B := Result and CN_FACTOR_AND <> 0;
Result := Result shl 1;
if B then
Result := Result xor CN_FACTOR_XOR;
if J in TBitMaksSet( ptr^ ) then
Result := Result xor CN_FACTOR_XOR;
end;
Inc( ptr );
end;
end;
function cal_crc( ptr : PByte; len : Byte ) : Cardinal;
var
i : Byte;
begin
Result := 0;
while len > 0 do
begin
i := $80;
while i <> 0 do
begin
if Result and $8000 <> 0 then
Result := ( Result shl 1 ) xor $8005
else
Result := Result shl 1;
if ptr^ and i <> 0 then
Result := Result xor $8005;
i := i shr 1;
end;
Inc( ptr );
Dec( len );
end;
end;
第二种
function cal_crc( ptr : PByte; len : Byte ) : Cardinal;
var
i, J, B : Byte;
begin
Result := 0;
for i := 1 to len do
begin
for J := 7 downto 0 do
begin
B := 1 shl J;
if Result and $8000 <> 0 then
Result := ( Result shl 1 ) xor $8005
else
Result := Result shl 1;
if ptr^ and B <> 0 then
Result := Result xor $8005;
end;
Inc( ptr );
end;
end;
第三种
type
TBitMask = 0..7;
TBitMaskSet = set of TBitMask;
function cal_crc( ptr : PByte; len : Byte ) : Cardinal;
const
CN_FACTOR_AND = $8000;
CN_FACTOR_XOR = $8005;
var
i, J : Byte;
B : Boolean;
begin
Result := 0;
for i := 1 to len do
begin
for J := 7 downto 0 do
begin
B := Result and CN_FACTOR_AND <> 0;
Result := Result shl 1;
if B then
Result := Result xor CN_FACTOR_XOR;
if J in TBitMaksSet( ptr^ ) then
Result := Result xor CN_FACTOR_XOR;
end;
Inc( ptr );
end;
end;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询