Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1275031
  • 博文数量: 129
  • 博客积分: 1449
  • 博客等级: 上尉
  • 技术积分: 3048
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-24 18:36
文章分类

全部博文(129)

文章存档

2015年(3)

2014年(20)

2013年(65)

2012年(41)

分类: C/C++

2013-02-14 09:45:04

C


unsigned char CRC8(unsigned char *ip, int i)
{
	unsigned char table_crc8[256] = {
		0,  94, 188,226,97, 63,221,131,194,156,126,32, 163,253,31, 65,
	  157,195,33, 127,252,162,64,30, 95, 1,  227,189,62, 96, 130,220,
	  35, 125,159,193,66, 28, 254,160,225,191,93, 3, 128,222,60, 98,
	  190,224,2,  92, 223,129,99, 61, 124,34, 192,158,29, 67,161,255,
	  70, 24, 250,164,39, 121,155,197,132,218,56,102,229,187,89, 7,
	  219,133,103,57, 186,228,6,  88, 25, 71,165,251,120,38, 196,154,
	  101,59, 217,135,4,  90, 184,230,167,249,27, 69, 198,152,122,36,
	  248,166,68, 26, 153,199,37, 123,58, 100,134,216,91, 5,  231,185,
	  140,210,48, 110,237,179,81, 15, 78, 16, 242,172,47, 113,147,205,
	  17, 79, 173,243,112,46, 204,146,211,141,111,49, 178,236,14, 80,
	  175,241,19, 77, 206,144,114,44, 109,51, 209,143,12, 82, 176,238,
	  50, 108,142,208,83, 13, 239,177,240,174,76, 18, 145,207,45, 115,
	  202,148,118,40, 171,245,23, 73, 8,  86, 180,234,105,55, 213,139,
	  87, 9,  235,181,54, 104,138,212,149,203,41, 119,244,170,72, 22,
	  233,183,85, 11, 136,214,52, 106,43, 117,151,201,74, 20,246,168,
	  116,42, 200,150,21, 75, 169,247,182,232,10, 84, 215,137,107,53	};
	unsigned char  crc_vault=0;
	int  j;

	for (j=0; j


51汇编

; ------------ 计算CRC8 ---------------
;入口: R0=数据起始地址, B=数据长度
;出口: A
;资源: R1,R6,R7,C
crc8:		
		mov r1,#0
	;	mov r0,a
	;	mov r2,b

dscrc8_2:	
		mov a,@r0
		mov r6,a
		mov r7,#8

dscrc8_1:	
		xrl a,r1	;crc_vault
		mov c,acc.0
		mov a,r1	;crc_vault
		jnc dscrc8_0
		xrl a,#18h

dscrc8_0:	
		rrc a
		mov r1,a
		mov a,r6
		rr a
		mov r6,a
		djnz r7,dscrc8_1
		inc r0
		djnz b,dscrc8_2
		mov a,r1

		ret




PIC汇编


CRC8:		clrf crc_buf
dscrc8_2:	movlw 8			;mov r7,#8
		movwf crc_r7

		movf INDF,0
		movwf crc_r6	;mov r6,a
			
dscrc8_1:	movf crc_r6,w
		xorwf crc_buf,w	;xrl a,r1	;crc_vault
		movwf crc_r5
		movf crc_buf,w
		btfsc crc_r5,0
		xorlw 0x18
		movwf crc_buf
		bcf STATUS,C
		btfsc crc_r5,0
		bsf STATUS,C
		rrf crc_buf,f

		bcf STATUS,C
		btfsc crc_r6,0
		bsf STATUS,C
		rrf crc_r6,f
		decfsz crc_r7,f
		goto dscrc8_1
		incf FSR,F
		decfsz crc_cont,1
		goto dscrc8_2
		movf crc_buf,w

		return



javascript


function CRC8(p, Num)		//p为数组传递参数, Num为数据的长度 
{ 
 var crcarray = [
	    0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65,
      157,195, 33,127,252,162, 64, 30, 95,  1,227,189, 62, 96,130,220,
       35,125,159,193, 66, 28,254,160,225,191, 93,  3,128,222, 60, 98,
      190,224,  2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255,
       70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89,  7,
      219,133,103, 57,186,228,  6, 88, 25, 71,165,251,120, 38,196,154,
      101, 59,217,135,  4, 90,184,230,167,249, 27, 69,198,152,122, 36,
      248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91,  5,231,185,
      140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205,
       17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80,
      175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238,
       50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115,
      202,148,118, 40,171,245, 23, 73,  8, 86,180,234,105, 55,213,139,
       87,  9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22,
      233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168,
      116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53]; 
 var temp,CRC8 = 0; 
 
 for(var i=0; i


php


//$p通过数组传递参数.
function CRC8($p, $Num)
{
 $crcarray = array(
	    0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65,
      157,195, 33,127,252,162, 64, 30, 95,  1,227,189, 62, 96,130,220,
       35,125,159,193, 66, 28,254,160,225,191, 93,  3,128,222, 60, 98,
      190,224,  2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255,
       70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89,  7,
      219,133,103, 57,186,228,  6, 88, 25, 71,165,251,120, 38,196,154,
      101, 59,217,135,  4, 90,184,230,167,249, 27, 69,198,152,122, 36,
      248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91,  5,231,185,
      140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205,
       17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80,
      175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238,
       50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115,
      202,148,118, 40,171,245, 23, 73,  8, 86,180,234,105, 55,213,139,
       87,  9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22,
      233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168,
      116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53);

 $CRC8 = 0;
 for($i=0; $i<$Num; $i++){
		$temp = $p[$i];
		$CRC8 = $crcarray[$CRC8 ^ $temp];
 }

 return($CRC8);
}


delphi


pb


crcarray={&  
		  0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65,&
      157,195, 33,127,252,162, 64, 30, 95,  1,227,189, 62, 96,130,220,&
       35,125,159,193, 66, 28,254,160,225,191, 93,  3,128,222, 60, 98,&
      190,224,  2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255,&
       70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89,  7,&
      219,133,103, 57,186,228,  6, 88, 25, 71,165,251,120, 38,196,154,&
      101, 59,217,135,  4, 90,184,230,167,249, 27, 69,198,152,122, 36,&
      248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91,  5,231,185,&
      140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205,&
       17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80,&
      175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238,&
       50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115,&
      202,148,118, 40,171,245, 23, 73,  8, 86,180,234,105, 55,213,139,&
       87,  9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22,&
      233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168,&
      116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53}

//func bitxor
//para x1,x2       // x1 和 x2 是十进制数,最后返回十进制数
string x,y,z
integer i

x = dectobin(x1)
y = dectobin(x2)     // 先变成二进制串,高位在前
z = ''
for i=1 to 8
	if mid(x,i,1)=mid(y,i,1) then   // 0⊙0=0  ,1⊙1=0
      z = z+'0'
   else            // 1⊙0=1  ,0⊙1=1
      z = z+'1'
  end if
next

return bintodec(z)


//调用例程
tmp = 0
for i = 1 To 19 //因为原定义下标必须为0-255,而现为1-256,所以此树组下标+1
	tmp = crcarray[bitxor(tmp,xust[i]) + 1]
next
xust[20] = tmp // 计算 CRC校验码




CRC32的C代码: 

点击(此处)折叠或打开

  1. #define     CRC32_Polynomial 0xEDB88320
  2. unsigned long GetCRC32(unsigned char *DataPtr, unsigned short DataNum)
  3. {
  4.     unsigned short    i, j;
  5.     unsigned long    CRC32 = 0xFFFFFFFF;    //初始值;
  6.     
  7.     for    (i=0; i<DataNum; i++) {
  8.         CRC32 ^= *DataPtr++;
  9.         for(j=0; j<8; j++) {
  10.          if((CRC32 & 0x00000001) != 0) {
  11.          CRC32 >>= 1;
  12.                 CRC32 ^= CRC32_Polynomial;
  13.             }
  14.             else {
  15.                 CRC32 >>= 1;
  16.             }
  17.         }
  18.     }
  19.     //return (CRC32 ^ 0xFFFFFFFF);        //取反
  20.     return CRC32;
  21. }

  22. //应用上次的CRC32的结果再次计算. 适合有限RAM的条件下多次连续计算
  23. unsigned long CRC32_xu(unsigned long CRC32, unsigned char *DataPtr, unsigned short DataNum)
  24. {
  25.     unsigned short    i, j;

  26.     for    (i=0; i<DataNum; i++) {
  27.         CRC32 ^= *DataPtr++;
  28.         for(j=0; j<8; j++) {
  29.          if((CRC32 & 0x00000001) != 0) {
  30.          CRC32 >>= 1;
  31.                 CRC32 ^= CRC32_Polynomial;
  32.             }
  33.             else {
  34.                 CRC32 >>= 1;
  35.             }
  36.         }
  37.     }
  38.     //return (CRC32 ^ 0xFFFFFFFF);        //取反
  39.     return CRC32;
  40. }



阅读(4808) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~