我有C#代码,用于计算发送到特定串行设备的命令的校验和字节。我需要帮助才能将此函数转换为Delphi 10.x for Windows和Delphi Android(如果它有所不同)。
C#代码
public byte CheckSum(byte[] btAryBuffer, int nStartPos, int nLen)
{
byte btSum = 0x00;
for (int nloop = nStartPos; nloop < nStartPos + nLen; nloop++ )
{
btSum += btAryBuffer[nloop];
}
return Convert.ToByte(((~btSum) + 1) & 0xFF);
}
Delphi代码
function CalcCheckSum(buffer: TArray<byte>; nStartPos: Integer; nLen: Integer): Byte;
var i: Integer;
begin
Result := 0;
i := nStartPos;
while i < nStartPos + nLen do
begin
Result := Result + buffer[i];
Inc(i);
end;
Result := ???
end;
答案 0 :(得分:4)
Result := ((not Result) + 1) and $FF;