CRC16算法之二:CRC16-CCITT-XMODEM算法的java实现
2024-09-29 13:14:38
CRC16算法系列文章:
前言
CRC16算法有很多种,本篇文章会介绍其中的CRC16-CCITT-XMODEM算法
功能
实现CRC16-CCITT-XMODEM算法
支持int、short类型
支持选择数组区域计算
实现
- package cc.eguid.crc16;
- /**
- * crc16多项式算法
- * @author eguid
- *
- */
- public class CRC16 {
- /**
- * CRC16-XMODEM算法(四字节)
- * @param bytes
- * @return
- */
- public static int crc16_ccitt_xmodem(byte[] bytes) {
- return crc16_ccitt_xmodem(bytes,0,bytes.length);
- }
- /**
- * CRC16-XMODEM算法(四字节)
- * @param bytes
- * @param offset
- * @param count
- * @return
- */
- public static int crc16_ccitt_xmodem(byte[] bytes,int offset,int count) {
- int crc = 0x0000; // initial value
- int polynomial = 0x1021; // poly value
- for (int index = offset; index < count; index++) {
- byte b = bytes[index];
- for (int i = 0; i < 8; i++) {
- boolean bit = ((b >> (7 - i) & 1) == 1);
- boolean c15 = ((crc >> 15 & 1) == 1);
- crc <<= 1;
- if (c15 ^ bit)
- crc ^= polynomial;
- }
- }
- crc &= 0xffff;
- return crc;
- }
- /**
- * CRC16-XMODEM算法(两字节)
- * @param bytes
- * @param offset
- * @param count
- * @return
- */
- public static short crc16_ccitt_xmodem_short(byte[] bytes,int offset,int count) {
- return (short)crc16_ccitt_xmodem(bytes,offset,count);
- }
- /**
- * CRC16-XMODEM算法(两字节)
- * @param bytes
- * @param offset
- * @param count
- * @return
- */
- public static short crc16_ccitt_xmodem_short(byte[] bytes) {
- return crc16_ccitt_xmodem_short(bytes,0,bytes.length);
- }
- }
---end---
最新文章
- Ubuntu上通过nginx部署Django笔记
- iOS开发——网络篇——UIWebview基本使用,NSInvocation(封装类),NSMethodSignature(签名),JavaScript,抛异常,消除警告
- C/C++ 如何来自动优雅的涮别银家的贴子
- 基于 IdentityServer3 实现 OAuth 2.0 授权服务【客户端模式(Client Credentials Grant)】
- sencha grid列tooltip提示
- 补间动画TweenAnimation
- UVa 11389 (贪心) The Bus Driver Problem
- How to install GSL on linux(ubuntu,centos,redhat)
- 如何使用KMS激活win10和office
- Linux下/etc/fstab文件详解
- md5sum.c, md5.c, md5.h
- malloc/free 的使用要点
- JavaSE学习入门
- C++ stl 怎么打印内存内容?
- SpringMVC实现用户登录实例
- 了解 JavaScript (2)- 需要了解的一些概念
- 70. Climbing Stairs爬楼梯
- git将一个分支的某个文件合并到当前分支
- Java 源程序与编译型运行区别
- 09 ORM 多表操作,创建表,添加记录
热门文章
- 数据挖掘之Slope One
- gulp入门-压缩js/css文件(windows)
- 一致性哈希算法(c#版)
- SQL Prompt 编辑
- 【nginx】关于Nginx的一些优化(突破十万并发)
- java 给多人发送、抄送
- 九度OJ 1157:中位数 (中位数、排序)
- 九度OJ 1058:反序输出 (基础题)
- PHP 格式化数字串
- Eclipse:Could not create the view: Plug-in org.eclipse.jdt.ui was unable to load class org.eclipse.