微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 如何计算校验和

我正在开发仪器驱动程序,我想知道如何计算帧校验和.

说明:

  1. Expressed by characters [0-9] and [A-F].

  2. Characters beginning from the character after [STX] and until [ETB] or
    [ETX] (including [ETB] or [ETX]) are added in binary.

  3. The 2-digit numbers,which represent the least significant 8 bits in
    hexadecimal code,are converted to ASCII characters [0-9] and [A-F].

  4. The most significant digit is stored in CHK1 and the least significant
    digit in 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");
}

原文地址:https://www.jb51.cc/csharp/95899.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐