栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

获取字节数组的CRC校验和并将其添加到该字节数组

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

获取字节数组的CRC校验和并将其添加到该字节数组

请使用以下代码:

// Compute the MODBUS RTU CRCprivate static int ModRTU_CRC(byte[] buf, int len){  int crc = 0xFFFF;  for (int pos = 0; pos < len; pos++) {    crc ^= (int)buf[pos] & 0xFF;   // XOR byte into least sig. byte of crc    for (int i = 8; i != 0; i--) {    // Loop over each bit      if ((crc & 0x0001) != 0) {      // If the LSB is set        crc >>= 1;         // Shift right and XOR 0xA001        crc ^= 0xA001;      }      else      // Else LSB is not set        crc >>= 1;         // Just shift right    }  }// Note, this number has low and high bytes swapped, so use it accordingly (or swap bytes)return crc;  }

但是,您可能必须反转返回的CRC才能获得正确的字节序。我什至在这里测试过:

http://ideone.com/PrBXVh

使用Windows计算器或其他工具,您可以看到第一个结果(来自上述函数调用)给出了预期值(尽管取反)。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/508802.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号