c# 2字节计算校验和

本文关键字:校验和 计算 字节 | 更新日期: 2023-09-27 18:12:06

我需要一个校验和计算的帮助。这是(不是我的代码!),但从规范http://www.leupamed.at/?wpdmact=process&做= NC5ob3RsaW5r

private void CalcCheckSum(string msg, out byte checksum1, out byte checksum2)
{
byte cs1 = 0;
byte cs2 = 0;
// Always use "'n" as line break when calculating the checksum.
msg = msg.Replace("'r'n", "'n"); // Find and replace CR LF with LF
msg = msg.Replace("'r", "'n"); // Find and replace CR with LF.
for (int i = 0; i < msg.Length; i++)
{
cs1 += (byte) msg[i];
cs2 += cs1;
}
checksum1 = cs1;
checksum2 = cs2;
}

我必须创建一个这样的包:

<!--:Begin:Chksum:1:--><!--:Ack:Msg:3:0:--><!--:End:Chksum:1:184:62:-->

字符串<!--:Ack:Msg:3:0:-->是实际数据,我必须计算两个校验和字节(184和62)并将它们插入最终数据包(如上所示)。

但是我的计算结果是10和62

var msg = "<!--:Ack:Msg:3:0:-->";
byte checksum1 = 0;
byte checksum2 = 0;
CalcCheckSum(msg, out checksum1, out checksum2);

我现在不知道如何计算正确的校验和值。这是响应的校验和。不用于验证请求。由于声誉不高,我无法上传图像,所以请查看规范中的最后一行:https://drive.google.com/file/d/0B_Gs9q9SJteadVRwSVc1a2FmUTg/edit?usp=sharing此确认消息是独立于请求的。只是它必须是对请求消息ID 3的响应。

解决方案吗?

校验和计算后:

checksum1 = 256 - (10 + 62) = 184

checksum2 = 62

设备通讯正常

c# 2字节计算校验和

校验和计算完成后:

checksum1 = 256 - (10 + 62) = 184

checksum2 = 62

可能这个问题太具体了,没有人有这种校验和计算的经验。设备通讯正常