最简单易懂的实现CRC16校验
2024-10-08 20:58:00
public String getCRC16(byte[] bytes) {
//CRC寄存器全为1
int CRC = 0x0000ffff;
//多项式校验值
int POLYNOMIAL = 0x0000a001;
int i, j;
for (i = 0; i < bytes.length; i++) {
CRC ^= ((int) bytes[i] & 0x000000ff);
for (j = 0; j < 8; j++) {
if ((CRC & 0x00000001) != 0) {
CRC >>= 1;
CRC ^= POLYNOMIAL;
} else {
CRC >>= 1;
}
}
}
//结果转换为16进制
String result = Integer.toHexString(CRC).toUpperCase();
if (result.length() != 4) {
StringBuffer sb = new StringBuffer("0000");
result = sb.replace(4 - result.length(), 4, result).toString();
}
//交换高低位
//return result.substring(2, 4) + result.substring(0, 2);//低前高后
return result;//高位在前,低位在后
}
最新文章
- bat脚本参数 if goto choice for使用的学习笔记。
- [转]<;jsp:include>;和<;%@include%>;的区别
- c#解析xml
- 【转】Android之自定义Adapter的ListView
- P6 EPPM Manual Installation Guide (Oracle Database)
- python2.7系列安装失败的办法
- safari的input问题
- Android屏幕大小适配问题解决
- android新浪分享实例
- 【转】判断URL是否能够访问
- java_web学习(2)Servlet
- iOS开发 - Swift使用JavaScriptCore与JS交互
- 解决GJson 获取web api数据出现Not a JsonObject问题
- ubuntu下zip文件操作
- Java自定义类加载器与双亲委派模型
- Spring security实现国际化问题
- <;<;梦断代码>;>;阅读笔记三
- MyEclipse上有main函数类运行报错:Editor does not contain a main type
- POJ 2607 Fire Station(Floyd打表+枚举更新最优)
- 数字签名算法(C#)