加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何计算校验和

发布时间:2020-12-15 06:18:53 所属栏目:百科 来源:网络整理
导读:我正在开发仪器驱动程序,我想知道如何计算帧校验和. 说明: 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 numbe
我正在开发仪器驱动程序,我想知道如何计算帧校验和.

说明:

  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");
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读