CRC16校验 Java版 0x180D

2023-12-26 15:12
文章标签 java 校验 crc16 0x180d

本文主要是介绍CRC16校验 Java版 0x180D,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

校验多项式为 0x180D,低字节在前,高字节在后
     public static String getCRC(byte[] data) {int length = data.length;byte crChi = (byte) 0xff;byte crCl = (byte) 0xff;int idx;for (int i = 0; i < length; i++) {idx = (crChi ^ data[i]) & 0xff;crChi = (byte) (crCl ^ gabyCRCHi[idx]);crCl = gabyCRCLo[idx];}int crc = (crChi << 8) | (crCl & 0xff);return HexUtils.encodeHEX(crc, false);}// 与校验域比较public static boolean equals(String crc, String checkCrc) {return Integer.parseInt(crc, 16) == Integer.parseInt(checkCrc, 16);}static final byte[] gabyCRCHi = {0x00, (byte) 0xc1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xc0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xc0, (byte)0x80, (byte) 0x41, (byte) 0x00, (byte) 0xc1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xc0, (byte) 0x80, (byte) 0x41, (byte)0x00, (byte) 0xc1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xc1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xc0, (byte)0x80, (byte) 0x41, (byte) 0x01, (byte) 0xc0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xc1, (byte) 0x81, (byte) 0x40, (byte)0x00, (byte) 0xc1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xc0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xc1, (byte)0x81, (byte) 0x40, (byte) 0x01, (byte) 0xc0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xc0, (byte) 0x80, (byte) 0x41, (byte)0x00, (byte) 0xc1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xc0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xc1, (byte)0x81, (byte) 0x40, (byte) 0x00, (byte) 0xc1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xc0, (byte) 0x80, (byte) 0x41, (byte)0x00, (byte) 0xc1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xc0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xc0, (byte)0x80, (byte) 0x41, (byte) 0x00, (byte) 0xc1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xc1, (byte) 0x81, (byte) 0x40, (byte)0x01, (byte) 0xc0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xc0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xc1, (byte)0x81, (byte) 0x40, (byte) 0x01, (byte) 0xc0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xc1, (byte) 0x81, (byte) 0x40, (byte)0x00, (byte) 0xc1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xc0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xc0, (byte)0x80, (byte) 0x41, (byte) 0x00, (byte) 0xc1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xc1, (byte) 0x81, (byte) 0x40, (byte)0x01, (byte) 0xc0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xc1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xc0, (byte)0x80, (byte) 0x41, (byte) 0x01, (byte) 0xc0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xc1, (byte) 0x81, (byte) 0x40, (byte)0x00, (byte) 0xc1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xc0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xc0, (byte)0x80, (byte) 0x41, (byte) 0x00, (byte) 0xc1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xc0, (byte) 0x80, (byte) 0x41, (byte)0x00, (byte) 0xc1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xc1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xc0, (byte)0x80, (byte) 0x41, (byte) 0x00, (byte) 0xc1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xc0, (byte) 0x80, (byte) 0x41, (byte)0x01, (byte) 0xc0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xc1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xc0, (byte)0x80, (byte) 0x41, (byte) 0x00, (byte) 0xc1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xc1, (byte) 0x81, (byte) 0x40, (byte)0x01, (byte) 0xc0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xc0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xc1, (byte)0x81, (byte) 0x40, (byte) 0x00, (byte) 0xc1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xc0, (byte) 0x80, (byte) 0x41, (byte)0x00, (byte) 0xc1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xc0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xc0, (byte)0x80, (byte) 0x41, (byte) 0x00, (byte) 0xc1, (byte) 0x81, (byte) 0x40};static final byte[] gabyCRCLo = {0x00, (byte) 0xc0, (byte) 0xc1, (byte) 0x01, (byte) 0xc3, (byte) 0x03, (byte) 0x02, (byte) 0xc2, (byte) 0xc6, (byte) 0x06, (byte)0x07, (byte) 0xc7, (byte) 0x05, (byte) 0xc5, (byte) 0xc4, (byte) 0x04, (byte) 0xcc, (byte) 0x0c, (byte) 0x0d, (byte) 0xcd, (byte)0x0f, (byte) 0xcf, (byte) 0xce, (byte) 0x0e, (byte) 0x0a, (byte) 0xca, (byte) 0xcb, (byte) 0x0b, (byte) 0xc9, (byte) 0x09, (byte)0x08, (byte) 0xc8, (byte) 0xd8, (byte) 0x18, (byte) 0x19, (byte) 0xd9, (byte) 0x1b, (byte) 0xdb, (byte) 0xda, (byte) 0x1a, (byte)0x1e, (byte) 0xde, (byte) 0xdf, (byte) 0x1f, (byte) 0xdd, (byte) 0x1d, (byte) 0x1c, (byte) 0xdc, (byte) 0x14, (byte) 0xd4, (byte)0xd5, (byte) 0x15, (byte) 0xd7, (byte) 0x17, (byte) 0x16, (byte) 0xd6, (byte) 0xd2, (byte) 0x12, (byte) 0x13, (byte) 0xd3, (byte)0x11, (byte) 0xd1, (byte) 0xd0, (byte) 0x10, (byte) 0xf0, (byte) 0x30, (byte) 0x31, (byte) 0xf1, (byte) 0x33, (byte) 0xf3, (byte)0xf2, (byte) 0x32, (byte) 0x36, (byte) 0xf6, (byte) 0xf7, (byte) 0x37, (byte) 0xf5, (byte) 0x35, (byte) 0x34, (byte) 0xf4, (byte)0x3c, (byte) 0xfc, (byte) 0xfd, (byte) 0x3d, (byte) 0xff, (byte) 0x3f, (byte) 0x3e, (byte) 0xfe, (byte) 0xfa, (byte) 0x3a, (byte)0x3b, (byte) 0xfb, (byte) 0x39, (byte) 0xf9, (byte) 0xf8, (byte) 0x38, (byte) 0x28, (byte) 0xe8, (byte) 0xe9, (byte) 0x29, (byte)0xeb, (byte) 0x2b, (byte) 0x2a, (byte) 0xea, (byte) 0xee, (byte) 0x2e, (byte) 0x2f, (byte) 0xef, (byte) 0x2d, (byte) 0xed, (byte)0xec, (byte) 0x2c, (byte) 0xe4, (byte) 0x24, (byte) 0x25, (byte) 0xe5, (byte) 0x27, (byte) 0xe7, (byte) 0xe6, (byte) 0x26, (byte)0x22, (byte) 0xe2, (byte) 0xe3, (byte) 0x23, (byte) 0xe1, (byte) 0x21, (byte) 0x20, (byte) 0xe0, (byte) 0xa0, (byte) 0x60, (byte)0x61, (byte) 0xa1, (byte) 0x63, (byte) 0xa3, (byte) 0xa2, (byte) 0x62, (byte) 0x66, (byte) 0xa6, (byte) 0xa7, (byte) 0x67, (byte)0xa5, (byte) 0x65, (byte) 0x64, (byte) 0xa4, (byte) 0x6c, (byte) 0xac, (byte) 0xad, (byte) 0x6d, (byte) 0xaf, (byte) 0x6f, (byte)0x6e, (byte) 0xae, (byte) 0xaa, (byte) 0x6a, (byte) 0x6b, (byte) 0xab, (byte) 0x69, (byte) 0xa9, (byte) 0xa8, (byte) 0x68, (byte)0x78, (byte) 0xb8, (byte) 0xb9, (byte) 0x79, (byte) 0xbb, (byte) 0x7b, (byte) 0x7a, (byte) 0xba, (byte) 0xbe, (byte) 0x7e, (byte)0x7f, (byte) 0xbf, (byte) 0x7d, (byte) 0xbd, (byte) 0xbc, (byte) 0x7c, (byte) 0xb4, (byte) 0x74, (byte) 0x75, (byte) 0xb5, (byte)0x77, (byte) 0xb7, (byte) 0xb6, (byte) 0x76, (byte) 0x72, (byte) 0xb2, (byte) 0xb3, (byte) 0x73, (byte) 0xb1, (byte) 0x71, (byte)0x70, (byte) 0xb0, (byte) 0x50, (byte) 0x90, (byte) 0x91, (byte) 0x51, (byte) 0x93, (byte) 0x53, (byte) 0x52, (byte) 0x92, (byte)0x96, (byte) 0x56, (byte) 0x57, (byte) 0x97, (byte) 0x55, (byte) 0x95, (byte) 0x94, (byte) 0x54, (byte) 0x9c, (byte) 0x5c, (byte)0x5d, (byte) 0x9d, (byte) 0x5f, (byte) 0x9f, (byte) 0x9e, (byte) 0x5e, (byte) 0x5a, (byte) 0x9a, (byte) 0x9b, (byte) 0x5b, (byte)0x99, (byte) 0x59, (byte) 0x58, (byte) 0x98, (byte) 0x88, (byte) 0x48, (byte) 0x49, (byte) 0x89, (byte) 0x4b, (byte) 0x8b, (byte)0x8a, (byte) 0x4a, (byte) 0x4e, (byte) 0x8e, (byte) 0x8f, (byte) 0x4f, (byte) 0x8d, (byte) 0x4d, (byte) 0x4c, (byte) 0x8c, (byte)0x44, (byte) 0x84, (byte) 0x85, (byte) 0x45, (byte) 0x87, (byte) 0x47, (byte) 0x46, (byte) 0x86, (byte) 0x82, (byte) 0x42, (byte)0x43, (byte) 0x83, (byte) 0x41, (byte) 0x81, (byte) 0x80, (byte) 0x40};public static void main(String[] args) {//这里写入需要生成校验码的字符串String str = "7C290113440311****30CCD8";byte[] data = HexUtils.hexStringToBytes(str);String crc = getCRC(data);//输出带有校验码的字符串System.out.println(crc);System.out.println(equals("02AD",crc));}

这篇关于CRC16校验 Java版 0x180D的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/539899

相关文章

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

Elasticsearch 在 Java 中的使用教程

《Elasticsearch在Java中的使用教程》Elasticsearch是一个分布式搜索和分析引擎,基于ApacheLucene构建,能够实现实时数据的存储、搜索、和分析,它广泛应用于全文... 目录1. Elasticsearch 简介2. 环境准备2.1 安装 Elasticsearch2.2 J

Java中的String.valueOf()和toString()方法区别小结

《Java中的String.valueOf()和toString()方法区别小结》字符串操作是开发者日常编程任务中不可或缺的一部分,转换为字符串是一种常见需求,其中最常见的就是String.value... 目录String.valueOf()方法方法定义方法实现使用示例使用场景toString()方法方法

Java中List的contains()方法的使用小结

《Java中List的contains()方法的使用小结》List的contains()方法用于检查列表中是否包含指定的元素,借助equals()方法进行判断,下面就来介绍Java中List的c... 目录详细展开1. 方法签名2. 工作原理3. 使用示例4. 注意事项总结结论:List 的 contain

Java实现文件图片的预览和下载功能

《Java实现文件图片的预览和下载功能》这篇文章主要为大家详细介绍了如何使用Java实现文件图片的预览和下载功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... Java实现文件(图片)的预览和下载 @ApiOperation("访问文件") @GetMapping("

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis