CRC16算法系列文章:

 

前言

CRC16算法有很多种,本篇文章会介绍其中的CRC16-CCITT-XMODEM算法

功能

实现CRC16-CCITT-XMODEM算法

支持int、short类型

支持选择数组区域计算

实现

  1. package cc.eguid.crc16;
  2.  
  3. /**
  4. * crc16多项式算法
  5. * @author eguid
  6. *
  7. */
  8. public class CRC16 {
  9.  
  10. /**
  11. * CRC16-XMODEM算法(四字节)
  12. * @param bytes
  13. * @return
  14. */
  15. public static int crc16_ccitt_xmodem(byte[] bytes) {
  16. return crc16_ccitt_xmodem(bytes,0,bytes.length);
  17. }
  18.  
  19. /**
  20. * CRC16-XMODEM算法(四字节)
  21. * @param bytes
  22. * @param offset
  23. * @param count
  24. * @return
  25. */
  26. public static int crc16_ccitt_xmodem(byte[] bytes,int offset,int count) {
  27. int crc = 0x0000; // initial value
  28. int polynomial = 0x1021; // poly value
  29. for (int index = offset; index < count; index++) {
  30. byte b = bytes[index];
  31. for (int i = 0; i < 8; i++) {
  32. boolean bit = ((b >> (7 - i) & 1) == 1);
  33. boolean c15 = ((crc >> 15 & 1) == 1);
  34. crc <<= 1;
  35. if (c15 ^ bit)
  36. crc ^= polynomial;
  37. }
  38. }
  39. crc &= 0xffff;
  40. return crc;
  41. }
  42.  
  43. /**
  44. * CRC16-XMODEM算法(两字节)
  45. * @param bytes
  46. * @param offset
  47. * @param count
  48. * @return
  49. */
  50. public static short crc16_ccitt_xmodem_short(byte[] bytes,int offset,int count) {
  51. return (short)crc16_ccitt_xmodem(bytes,offset,count);
  52. }
  53. /**
  54. * CRC16-XMODEM算法(两字节)
  55. * @param bytes
  56. * @param offset
  57. * @param count
  58. * @return
  59. */
  60. public static short crc16_ccitt_xmodem_short(byte[] bytes) {
  61. return crc16_ccitt_xmodem_short(bytes,0,bytes.length);
  62. }
  63.  
  64. }

---end---

最新文章

  1. Ubuntu上通过nginx部署Django笔记
  2. iOS开发——网络篇——UIWebview基本使用,NSInvocation(封装类),NSMethodSignature(签名),JavaScript,抛异常,消除警告
  3. C/C++ 如何来自动优雅的涮别银家的贴子
  4. 基于 IdentityServer3 实现 OAuth 2.0 授权服务【客户端模式(Client Credentials Grant)】
  5. sencha grid列tooltip提示
  6. 补间动画TweenAnimation
  7. UVa 11389 (贪心) The Bus Driver Problem
  8. How to install GSL on linux(ubuntu,centos,redhat)
  9. 如何使用KMS激活win10和office
  10. Linux下/etc/fstab文件详解
  11. md5sum.c, md5.c, md5.h
  12. malloc/free 的使用要点
  13. JavaSE学习入门
  14. C++ stl 怎么打印内存内容?
  15. SpringMVC实现用户登录实例
  16. 了解 JavaScript (2)- 需要了解的一些概念
  17. 70. Climbing Stairs爬楼梯
  18. git将一个分支的某个文件合并到当前分支
  19. Java 源程序与编译型运行区别
  20. 09 ORM 多表操作,创建表,添加记录

热门文章

  1. 数据挖掘之Slope One
  2. gulp入门-压缩js/css文件(windows)
  3. 一致性哈希算法(c#版)
  4. SQL Prompt 编辑
  5. 【nginx】关于Nginx的一些优化(突破十万并发)
  6. java 给多人发送、抄送
  7. 九度OJ 1157:中位数 (中位数、排序)
  8. 九度OJ 1058:反序输出 (基础题)
  9. PHP 格式化数字串
  10. Eclipse:Could not create the view: Plug-in org.eclipse.jdt.ui was unable to load class org.eclipse.