Delphi中的按位补码。 (翻译C#〜运算符)

问题描述 投票:0回答:1

我有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);
}

德尔福代码

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;
c# delphi serial-port
1个回答
4
投票
 Result := ((not Result) + 1) and $FF;
© www.soinside.com 2019 - 2024. All rights reserved.