CRC16算法之二:www.shyejk.com/算法的java实现

  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_www.shyejk.com/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_www.shyejk.com/short(bytes,0,bytes.length);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值