我正在为 Laboratory Information System 开发仪器驱动程序.我想知道如何计算帧的校验和。
校验和算法的解释:
Expressed by characters [0-9] and [A-F].
Characters beginning from the character after [STX] and until [ETB] or [ETX] (including [ETB] or [ETX]) are added in binary.
The 2-digit numbers, which represent the least significant 8 bits in hexadecimal code, are converted to ASCII characters [0-9] and [A-F].
The most significant digit is stored in CHK1 and the least significant digit in CHK2.
我没有得到上面的第 3 点和第 4 点。
这是一个示例框架:
<STX>2Q|1|2^1||||20011001153000<CR><ETX><CHK1><CHK2><CR><LF>
CHK1
和CHK2
的值是多少?如何在 C# 中实现给定的算法?
最佳答案
终于得到答案,这里是计算校验和的代码:
private string CalculateChecksum(string dataToCalculate)
{
byte[] byteToCalculate = Encoding.ASCII.GetBytes(dataToCalculate);
int checksum = 0;
foreach (byte chData in byteToCalculate)
{
checksum += chData;
}
checksum &= 0xff;
return checksum.ToString("X2");
}
关于c# - 计算实验室信息系统 (LIS) 帧的校验和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10947717/