CRC16算法之二:CRC16-CCITT-XMODEM算法的java实现

CRC16算法系列文章:

CRC16算法之一:CRC16-CCITT-FALSE算法的java实现

CRC16算法之二:CRC16-CCITT-XMODEM算法的java实现

CRC16算法之三:CRC16-CCITT-MODBUS算法的java实现

前言

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---

原文地址:https://www.cnblogs.com/eguid/p/9667140.html

时间: 2024-07-29 05:12:18

CRC16算法之二:CRC16-CCITT-XMODEM算法的java实现的相关文章

[计算机图形学 with OpenGL] Chapter8 习题8.12 NLN二维线段裁剪算法实现

Nicholl-Lee-Nicholl二维线段裁剪算法相对于Cohen-Sutherland和Liang-Barsky算法来说,在求交点之前进行了线段端点相对于几个区域的判断,可以确切的知道要求交点的边的信息. 此方法只在二维空间裁剪时使用,C-S和L-B裁剪方法则可应用到三维空间. 算法步骤: 1 先使用C-S裁剪算法的区域码判断方法,去除一部分在裁剪区域外面的线段.显示在完全在裁剪区域内的线段.其他不能判断的情况,采用NLN算法进行裁剪. 2 p1和p2若有一点在区域内,必要时交换端点以确保

排序算法--(二)

选择排序 直接选择排序: 选择排序,每一趟找到一个最小(大)值,每一趟遍历的数据减少一次. template <typename T> void SelectSort(T a[],int length) { T temp; for (int i=0;i<length;i++) { int k =i; for (int j=i+1;j<length;j++) { if (a[j]<a[k]) k=j; //这里只是对比它小的元素中最小的位置进行标记,每次相当于最多移动一次. }

每日算法之二十八:Longest Valid Parentheses

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example is &

每日算法之二十五:Divide Two Integers

Divide two integers without using multiplication, division and mod operator. 不使用乘法.除法和求模运算求两个数相除. class Solution { public: long long internalDivide(unsigned long long dividend,unsigned long long divisor) { if(dividend<divisor) return 0; int result =

每日算法之二十二:Swap Nodes in Pairs

Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list, on

每日算法之二十三:Reverse Nodes in k-Group

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nod

每日算法之二十六:Substring with Concatenation of All Words

变相的字符串匹配 给定一个字符串,然后再给定一组相同长度的单词列表,要求在字符串中查找满足以下条件的起始位置: 1)从这个位置开始包含单词列表中所有的单词,且每个单词仅且必须出现一次. 2)在出现的过程中不能出现其他的干扰单词. 3)出现的位置可能有多个. 4)单词的出现顺序不做要求. 下面是一个例子: S:"barfoothefoobarman" L:"foo","bar" 位置0是出现位置,:两个单词均出现仅出现一次,且没有干扰.同样位置9也

每日算法之二十九:Search in Rotated Sorted Array

在一个经过旋转后的有序数组中查找一个目标元素. Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1.

隐马尔可夫模型HMM与维特比Veterbi算法(二)

隐马尔可夫模型HMM与维特比Veterbi算法(二) 主要内容: 前向算法(Forward Algorithm) 穷举搜索( Exhaustive search for solution) 使用递归降低问题复杂度 前向算法的定义 程序实现前向算法 举例说明前向算法 一.前向算法(Forward Algorithm) 目标:计算观察序列的概率(Finding the probability of an observed sequence) 1. 穷举搜索( Exhaustive search fo