1、工具类

import java.math.BigInteger;/*** CRC校验* @author xl**/
public class CRC16Util {static byte[] crc16_tab_h = { (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) 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 byte[] crc16_tab_l = { (byte) 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 };/*** 计算CRC16校验* * @param data*            需要计算的数组* @return CRC16校验值*/public static int calcCrc16(byte[] data) {return calcCrc16(data, 0, data.length);}/*** 计算CRC16校验* * @param data*            需要计算的数组* @param offset*            起始位置* @param len*            长度* @return CRC16校验值*/public static int calcCrc16(byte[] data, int offset, int len) {return calcCrc16(data, offset, len, 0xffff);}/*** 计算CRC16校验* * @param data*            需要计算的数组* @param offset*            起始位置* @param len*            长度* @param preval*            之前的校验值* @return CRC16校验值*/public static int calcCrc16(byte[] data, int offset, int len, int preval) {int ucCRCHi = (preval & 0xff00) >> 8;int ucCRCLo = preval & 0x00ff;int iIndex;for (int i = 0; i < len; ++i) {iIndex = (ucCRCLo ^ data[offset + i]) & 0x00ff;ucCRCLo = ucCRCHi ^ crc16_tab_h[iIndex];ucCRCHi = crc16_tab_l[iIndex];}return ((ucCRCLo & 0x00ff) << 8) | (ucCRCHi & 0x00ff) & 0xffff;}// 测试public static void main(String[] args) {// 0x02 05 00 03 FF 00 , crc16=7C 09int crc = CRC16Util.calcCrc16(new byte[] { 0x01, 0x06, 0x00, 0x00,0x00, 0x01 });int crc2 = CRC16Util.calcCrc16(new byte[] { 0x01, 0x06, 0x20, 0x01,0x00, 0x01 });String str = "010600000001";String str2 = "010620010001";byte[] bstr = new BigInteger(str, 16).toByteArray();// for(int i=0;i<bstr.length;i++){// System.out.println(i+"="+bstr[i]);// }//byte[] bstr2 = new BigInteger(str2, 16).toByteArray();// for(int i=0;i<bstr2.length;i++){// System.out.println(i+"="+bstr2[i]);// }System.out.println(String.format("0x%04x", CRC16Util.calcCrc16(bstr)));System.out.println(String.format("0x%04x", CRC16Util.calcCrc16(bstr2)));System.out.println(String.format("%04x", crc));System.out.println(String.format("0x%04x", crc2));}}

2、使用

    /*** 获取payload** @return*/private static String getPayload(String addrk, int value) {int addr = Integer.parseInt(addrk.substring(1, addrk.length()));String A = PAY_CONTENT_CMD + String.format("%04x", addr).toUpperCase()+ String.format("%04x", value).toUpperCase();byte[] bA = new BigInteger(A, 16).toByteArray();String B = String.format("%04x", CRC16Util.calcCrc16(bA));return PAY_HEAD + A + B + PAY_END;}

完!!!

JAVA CRC16校验工具相关推荐

  1. Java身份证校验工具类

    Java身份证校验工具类 工作中经常会涉及到身份证的校验,而且需求不同,有的需要校验最后一位校验位,有的不需要,这里参考了几篇文章及自己工作中用到的情况写了一个工具类.记录一下,菜鸟一个,大佬勿喷. ...

  2. Java正则表达式校验工具类_【Java工具类】----正则表达式校验工具类

    /** * @Title: RegexValidateUtil.java * @Package org.csun.nc.util * @Description: TODO * @author chis ...

  3. JAVA CRC16校验

    方法一 /*** 计算CRC16校验码** @param bytes* @return*/public static String getCRC(byte[] bytes) {int CRC = 0x ...

  4. java 逻辑校验工具_SpringBoot2.0实战(10)整合fluent-validator优雅业务校验

    相关知识 FluentValidator是一个工具类库,使用流式(Fluent Interface)调用风格让校验跑起来更优雅,代码更简洁,同时验证器(Validator)可以做到开闭原则,实现最大程 ...

  5. JAVA续本_Java业务校验工具实现(续集)

    一.背景 在前面的文章分享了一篇自已关于Java业务校验工具的实现Java业务校验工具实现,后面本着"不要重复造轮子"的原则,在网上搜索果然有志同道合的朋友已经实现过相同的功能框架 ...

  6. Java实现crc16校验 附上校验工具对照。解决长数据校验不正确的问题

    Android与底层硬件通信,进行CRC16校验时,要考虑到无符号整形的原因,java会将超过的值变成负数 C算法: Uint16 crc16 = 0; Uint8  i = 0; Uint16 Cr ...

  7. JAVA CRC-16(CRC-16/IBM)计算校验值

    JAVA CRC-16(CRC-16/IBM)计算校验值 前言:对接硬件设备的通信协议需要用到CRC-16进行计算校验值,然后再网上找了很多个版本,很久都没找到这个版本的,然后好不容易找到了,就记录一 ...

  8. Java实现CRC16校验算法

    本文主要记录java版的crc校验算法,顺带贴上c的crc校验函数 先上c的校验函数,本文java版的校验就是根据此函数而来的,希望能帮到大家 当时开发时通过一个在线检验工具辅助开发的.工具链接戳我 ...

  9. java validate校验_自定义工具类实现validate参数校验

    前言 相信项目中做一些htttp接口,避免不了要对参数进行校验,大多数情况下,其实我们只是校验是否为NULL就可以了 1.通过注解实现各种状态的字段 1.1.引入依赖 默认的版本是6.0.9.Fina ...

最新文章

  1. 动态多尺度图表达3D人体骨架运动,实现精准预测效果超SOTA
  2. MATLAB报错“Integers can only be raised to positive integral powers.“
  3. c++ opencv编程实现暗通道图像去雾算法_OpenCV图像处理专栏十五 |一种基于亮度均衡的图像阈值分割技术...
  4. golang GOPROXY及GOPRIVATE的设置及作用
  5. BZOJ2843:极地旅行社
  6. Visual Studio 2008在设计视图和代码视图切换的快捷键F7
  7. A1078. 逆序对个数
  8. 江苏单招试题计算机原理及答案,江苏省对口单招计算机组装与维修计算机原理考题分类汇总.docx...
  9. java线程立刻执行_Java 线程调用start()后会立即执行run()方法吗?
  10. 怎么看作业部落的html,HTML问题汇总
  11. 使用RMAN恢复目录(catalog)解析
  12. Eclipse的汉化
  13. Android源码刷机步骤
  14. 在登陆Fedora 9时选择语言
  15. 普中28335开发攻略_凌乱的DSP笔记(1)-F28335基础知识
  16. php实现pdf转图片
  17. CTS 使用instrumentation命令测试
  18. 使用VS2015+win7编译WebKit(WebKit-r189384)
  19. data[i] is underfined
  20. 空间大数据相关的技术

热门文章

  1. 紫外线擦除器使用方法_如何制作紫外线擦除器
  2. Linux配置Java环境变量(添加JDK至环境变量)
  3. 计算机网络存在异常请,win10系统电脑网络出现异常的解决方法
  4. OpenType™ Layout通用表格式
  5. SQLServer中,sa账号旁边有红色小箭头
  6. 前端自动化构建工具--Plop
  7. win10便笺怎么添加图片,win10便签插入图片步骤分享
  8. 快速便捷的了解KVM虚拟化
  9. 安装TTYLINUX的尝试
  10. Python的turtle库(教师节祝福,用turtle画一只熊猫和一个太阳)