CCITT标准CRC16(1021) Java代码

来源:互联网 发布:it男 编辑:程序博客网 时间:2024/05/22 00:48

背景:

与GPS运营商做数据对接,图片、GPS、轨迹所有数据数据包中都有CRC校验值,需要对数据包进行校验。

Java代码:

[java] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. package com.jst.util;  
  2.   
  3. /** 
  4.  * CRC-CCITT 算法校验类 
  5.  *  
  6.  * @author amadowang 
  7.  * @version [版本号, Aug 29, 2011] 
  8.  * @see [相关类/方法] 
  9.  * @since [产品/模块版本] 
  10.  * @date 2012-10-24 
  11.  */  
  12. public class CRCUtil {  
  13.     /* 
  14.      * CCITT标准CRC16(1021)余数表 CRC16-CCITT ISO HDLC, ITU X.25, x16+x12+x5+1 多项式 
  15.      * 高位在先时生成多项式 Gm=0x11021 低位在先时生成多项式,Gm=0x8408 本例采用高位在先 
  16.      */  
  17.     private static int crc16_ccitt_table[] = { 0x00000x10210x20420x30630x40840x50a50x60c60x70e70x81080x91290xa14a,  
  18.             0xb16b0xc18c0xd1ad0xe1ce0xf1ef0x12310x02100x32730x22520x52b50x42940x72f70x62d60x93390x83180xb37b,  
  19.             0xa35a0xd3bd0xc39c0xf3ff0xe3de0x24620x34430x04200x14010x64e60x74c70x44a40x54850xa56a0xb54b0x8528,  
  20.             0x95090xe5ee0xf5cf0xc5ac0xd58d0x36530x26720x16110x06300x76d70x66f60x56950x46b40xb75b0xa77a0x9719,  
  21.             0x87380xf7df0xe7fe0xd79d0xc7bc0x48c40x58e50x68860x78a70x08400x18610x28020x38230xc9cc0xd9ed0xe98e,  
  22.             0xf9af0x89480x99690xa90a0xb92b0x5af50x4ad40x7ab70x6a960x1a710x0a500x3a330x2a120xdbfd0xcbdc0xfbbf,  
  23.             0xeb9e0x9b790x8b580xbb3b0xab1a0x6ca60x7c870x4ce40x5cc50x2c220x3c030x0c600x1c410xedae0xfd8f0xcdec,  
  24.             0xddcd0xad2a0xbd0b0x8d680x9d490x7e970x6eb60x5ed50x4ef40x3e130x2e320x1e510x0e700xff9f0xefbe0xdfdd,  
  25.             0xcffc0xbf1b0xaf3a0x9f590x8f780x91880x81a90xb1ca0xa1eb0xd10c0xc12d0xf14e0xe16f0x10800x00a10x30c2,  
  26.             0x20e30x50040x40250x70460x60670x83b90x93980xa3fb0xb3da0xc33d0xd31c0xe37f0xf35e0x02b10x12900x22f3,  
  27.             0x32d20x42350x52140x62770x72560xb5ea0xa5cb0x95a80x85890xf56e0xe54f0xd52c0xc50d0x34e20x24c30x14a0,  
  28.             0x04810x74660x64470x54240x44050xa7db0xb7fa0x87990x97b80xe75f0xf77e0xc71d0xd73c0x26d30x36f20x0691,  
  29.             0x16b00x66570x76760x46150x56340xd94c0xc96d0xf90e0xe92f0x99c80x89e90xb98a0xa9ab0x58440x48650x7806,  
  30.             0x68270x18c00x08e10x38820x28a30xcb7d0xdb5c0xeb3f0xfb1e0x8bf90x9bd80xabbb0xbb9a0x4a750x5a540x6a37,  
  31.             0x7a160x0af10x1ad00x2ab30x3a920xfd2e0xed0f0xdd6c0xcd4d0xbdaa0xad8b0x9de80x8dc90x7c260x6c070x5c64,  
  32.             0x4c450x3ca20x2c830x1ce00x0cc10xef1f0xff3e0xcf5d0xdf7c0xaf9b0xbfba0x8fd90x9ff80x6e170x7e360x4e55,  
  33.             0x5e740x2e930x3eb20x0ed10x1ef0 };  
  34.   
  35.   
  36.     /** 
  37.      *  
  38.      * @param reg_init CRC校验时初值 
  39.      * @param message 校验值 
  40.      * @return 
  41.      */  
  42.     private static int do_crc(int reg_init, byte[] message) {  
  43.         int crc_reg = reg_init;  
  44.         for (int i = 0; i < message.length; i++) {  
  45.             crc_reg = (crc_reg >> 8) ^ crc16_ccitt_table[(crc_reg ^ message[i]) & 0xff];  
  46.         }  
  47.         return crc_reg;  
  48.     }  
  49.   
  50.     /** 
  51.      * 根据数据生成CRC校验码 
  52.      *  
  53.      * @param message 
  54.      *            byte数据 
  55.      *  
  56.      * @return int 返校验码 
  57.      */  
  58.     private static int do_crc(byte[] message) {  
  59.         // 计算CRC校验时初值从0x0000开始。  
  60.         int crc_reg = 0x0000;  
  61.         return do_crc(crc_reg, message);  
  62.     }  
  63.     /** 
  64.      * db44检验方法 
  65.      *  
  66.      * @param message 消息内容 
  67.      * @param crc 检验码值 
  68.      * @return 
  69.      */  
  70.     private static boolean do_crc(byte[] message,byte[] crc) {  
  71.         // 计算CRC校验时初值从0x0000开始。  
  72.         int crc_reg = 0x0000;  
  73.         int crc_value = (crc[0]&0xff)*256+(crc[1]&0xff);  
  74.         return crc_value ==do_crc(crc_reg, message);  
  75.     }  
  76.     /** 
  77.      * 供db44结构代码使用,数组后两位为CRC-校验码值 
  78.      * @param messages 
  79.      * @return 
  80.      */  
  81.     public static boolean do_crc_db44(byte[] messages) {  
  82.         // 计算CRC校验时初值从0x0000开始。  
  83.         byte[] messageArray = new byte[messages.length-2];  
  84.         byte[] crcArray = new byte[2];  
  85.         System.arraycopy(messages, 0, messageArray, 0, messageArray.length);  
  86.         System.arraycopy(messages, messages.length-2, crcArray, 02);  
  87.         return do_crc(messageArray, crcArray);  
  88.     }  
  89.   
  90.     public static void main(String[] args) {  
  91.         // 一个db44测试样本数据  
  92.         byte p[] = {71,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,68,31,32,0,1,2};  
  93.         int crc = do_crc(p); // 计算前两位的CRC码  
  94.         // 65336  
  95.         System.out.println(crc);  
  96.           
  97.     }  
  98.   
  99. }  


原文地址:http://blog.csdn.net/javaee_ssh/article/details/27202753


0 0