c#2字节计算校验和

问题描述 投票:-5回答:1

校验和计算需要帮助。这是(不是我的代码!),但来自规范http://www.leupamed.at/?wpdmact=process&did=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# checksum
1个回答
0
投票

计算校验和后:

checksum1 = 256 - (10 + 62)= 184

checksum2 = 62

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

© www.soinside.com 2019 - 2024. All rights reserved.