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.pswp.cn/news/274200.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/274200.shtml
英文地址,請注明出處:http://en.pswp.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;并對用戶界面作出了多處修改。雖然…