JAVA编程心得-JAVA实现CRC-CCITT(XMODEM)算法

    CRC即循环冗余校验码(Cyclic Redundancy Check):是数据通信领域中最常用的一种差错校验码,其特征是信息字段和校验字段的长度可以任意选定。

 

          1 byte checksum 

          CRC-16    

          CRC-16 (Modbus)  
          CRC-16 (Sick)  
          CRC-CCITT (XModem)  
          CRC-CCITT (0xFFFF)

          CRC-CCITT (0x1D0F)

          CRC-CCITT (Kermit)  
          CRC-DNP          

          CRC-32

这里我以CRC-CCITT (XModem)  为例,分别用计算方法与查表法来实现

1.计算法

 

		public static int CRC_XModem(byte[] bytes){int crc = 0x00;          // initial valueint polynomial = 0x1021;  for (int index = 0 ; index< bytes.length; 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;	 }

 

 

2.查表法:

 

 static final char TABLE1021[] = { /* CRC1021余式表 */  0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, 0x8108,   0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, 0x1231,   0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6, 0x9339,   0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de, 0x2462,   0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485, 0xa56a,   0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d, 0x3653,   0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4, 0xb75b,   0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc, 0x48c4,   0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823, 0xc9cc,   0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b, 0x5af5,   0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12, 0xdbfd,   0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a, 0x6ca6,   0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41, 0xedae,   0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49, 0x7e97,   0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70, 0xff9f,   0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78, 0x9188,   0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f, 0x1080,   0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067, 0x83b9,   0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e, 0x02b1,   0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256, 0xb5ea,   0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d, 0x34e2,   0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, 0xa7db,   0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c, 0x26d3,   0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634, 0xd94c,   0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab, 0x5844,   0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3, 0xcb7d,   0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a, 0x4a75,   0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92, 0xfd2e,   0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9, 0x7c26,   0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1, 0xef1f,   0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8, 0x6e17,   0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0 };   public static char getCRC1021(byte b[], int len) {   char crc = 0;   byte hb = 0;   int j = 0;   int index;   while (len-- != 0) {   hb = (byte) (crc / 256); //以8位二进制数的形式暂存CRC的高8位   index = ((hb ^ b[j]) & 0xff); //求得所查索引下标   crc <<= 8; // 左移8位,相当于CRC的低8位乘以   crc ^= (TABLE1021[index]); // 高8位和当前字节相加后再查表求CRC ,再加上以前的CRC   j++;   }   return (crc);   }


3.测试方法与结果:

 

 

	public static void main(String args[]) {byte[] b = new byte[] {(byte) 0x2C, (byte) 0x00, (byte) 0xFF, (byte) 0xFE,(byte) 0xFE, (byte) 0x04, (byte) 0x00, (byte) 0x00,(byte) 0x00, (byte) 0x00 };int a = getCRC1021(b, 10);System.out.println("查表法:" + a);String str = Integer.toHexString(a).toUpperCase();System.out.println("十六进制:" + str);System.out.println("计算法:" + CRC_XModem(b));System.out.println("十六进制:"+ Integer.toHexString(CRC_XModem(b)).toUpperCase());}


结果:

 




 

 

转载于:https://www.cnblogs.com/snake-hand/p/3143034.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/274200.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

什么字体字母和数字大小一样_字母和字体如何适应我们的屏幕

什么字体字母和数字大小一样Writing went through many iterations before it became what is today. Times New Roman wasn’t the default script for ancient Egyptians, in fact, paper didn’t even exist when the first words were written.写作经历了许多迭代&#xff…

jenkins 通过批处理自动构建 非标准项目

之前介绍了java和vs2010的项目构建&#xff0c;这些都是比较常见的&#xff0c;所以都用专门的工具。但但难免会遇到一些不常见的项目&#xff0c;下面介绍通过批处理进行构建&#xff0c;并用jenkins调用.我们这里使用plc语言&#xff0c;没有标准环境&#xff0c;只有使用bat…

效果图底图 线框图_5分钟的线框图教程

效果图底图 线框图为什么使用线框&#xff1f; (Why wireframe?) Simply put, wireframes provide a structure and layout for content and assets.简而言之&#xff0c;线框提供了内容和资产的结构和布局。 You can wireframe just about any kind of presentation, from p…

多线程 - 你知道线程栈吗

问题 1. local 变量的压栈和出栈过程 void func1(){ int a 0; int b 0; } 系统中有一个栈顶指针&#xff0c;每次分配和回收local 变量时&#xff0c;其实就是移动栈指针。 2. static local变量的分配风险 void func2(){ static int a 0; } 这个变量a可能会被分…

怎么让qt发声_第3部分:添加网络字体-让我们的单词发声

怎么让qt发声This is a big week for the project. While it was an important step last week to establish some basic responsiveness, we couldn’t really nail down the typography until we added the typeface. Too many aspects of the feel, proportions, and overal…

mysql语句中把string类型字段转datetime类型

mysql语句中把string类型字段转datetime类型在mysql里面利用str_to_date&#xff08;&#xff09;把字符串转换为日期此处以表h_hotelcontext的Start_time和End_time字段为例&#xff0c;查询当前时间在此范围之内的数据。 www.2cto.com select * from h_hotelcontext where …

名词解释:对等知识互联网_网站设计理论:比较和对等

名词解释:对等知识互联网Equivalence and contrast, connection and distinction, categorization and non-categorization are all ways to distinguish the same or different elements. Based on the information they carry, we hope that the equivalent elements can hav…

hadoop深入研究:(五)——Archives

转载请注明来源地址&#xff1a;http://blog.csdn.net/lastsweetop/article/details/9123155 简介 我们在hadoop深入研究:(一)——hdfs介绍里已讲过&#xff0c;hdfs并不擅长存储小文件&#xff0c;因为每个文件最少一个block&#xff0c;每个block的元数据都会在namenode节点占…

人民币小写金额转大写金额

#region 人民币小写金额转大写金额/// <summary>/// 小写金额转大写金额/// </summary>/// <param name"Money">接收需要转换的小写金额</param>/// <returns>返回大写金额</returns>public static string ConvertMoney(Decimal…

饥饿的盛世读后感_满足任何设计师饥饿感的原型制作工具

饥饿的盛世读后感Tell me if this story sounds familiar to you. You just wrapped up a design in Sketch -a design that took you hours, and now you want to bring it to life. Sketch’s built-in prototyping tool doesn’t allow you to create all the interactions …

关于软件版本的说明

Trial&#xff1a;试用版&#xff0c;软件在功能或时间上有所限制&#xff0c;如果想解除限制&#xff0c;需要购买零售版。 Retail&#xff1a;零售版。Free&#xff1a;免费版。Full&#xff1a;完全版。Alpha&#xff1a;内部测试版&#xff0c;通常在Beta版发布之前推出。…

figma 安装插件_我制作Figma插件的经验

figma 安装插件Since Figma released the Figma Community (Beta), I’ve been working on Figma plugins in my free time while I study the code. With the help of an engineer friend of mine, I’ve developed four small plugins so far. As I continue to update these…

术语解释_术语

术语解释Colour has a great impact in the world around us and this is no different in User Interfaces (UI). However, it’s not always given the importance it deserves. Sometimes colour is understood as a purely aesthetic element that is completely relative …

安卓中的对话框通知---简单的对话框入门

当你的应用需要显示一个进度条或需要用户对信息进行确认时&#xff0c;可以使用对话框来完成。 1、用一个按钮来进行测试&#xff0c;在layout文件中的activity_main.xml文件中添加一个Button按钮&#xff1a; <RelativeLayout xmlns:android"http://schemas.android.c…

mac photoshop_我讨厌Photoshop…

mac photoshopIt probably sounds odd to hear a visual designer say they hate Photoshop. It’s sort of like hearing a writer say they hate Word. It’s sort of a given that Photoshop is the medium within which visual designers work their magic. It’s also one…

PHP中的ob_start用法详解

用PHP的ob_start();控制您的浏览器cache Output Control 函数可以让你自由控制脚本中数据的输出。它非常地有用&#xff0c;特别是对于&#xff1a;当你想在数据已经输出后&#xff0c;再输出文件头的情况。输出控制函数不对使用 header() 或 setcookie(), 发送的文件头信息产生…

做事用人 用人做事_做事:构建我的第一个Web应用程序的经验教训

做事用人 用人做事On the 5th of June, 2020, after almost two weeks of (re)learning javascript, fixing bugs, creating new ones and of course, lots of testing, I launched Writty on ProductHunt. An open-source text editor to help anyone who is into writing to …

[转]C#委托的异步调用

本文将主要通过“同步调用”、“异步调用”、“异步回调”三个示例来讲解在用委托执行同一个“加法类”的时候的的区别和利弊。 首先&#xff0c;通过代码定义一个委托和下面三个示例将要调用的方法&#xff1a; /*添加的命名空间using System.Threading;using System.Runtime.…

vista下载_Vista和视图在游戏设计中的功能

vista下载Views in video games are observation points used to highlight a lot of objects into one frame or shot using a special camera move. Vistas are special types of views that show distant objects, mainly far off landscapes.电子游戏中的视图是观察点&…

微软开始提供公共预览版Windows 8.1下载

用户可在微软发布官方更新时免费下载Windows 8.1&#xff0c;这个最新版本的Windows 8系统对搜索系统作出了改进&#xff0c;此外还修改了Windows Store&#xff0c;并对核心应用进行了升级。Windows 8.1还重新推出了“开始”按钮&#xff0c;并对用户界面作出了多处修改。虽然…