import java.io.*;
import java.util.*;
class MyClass{
char crc16(int data[],int size){
char crc = 0x0;
int i,j;
int data_t ;
i = j = 0;
if(data == null) return 0;
for(j=0;j<size;j++){
data_t = data[j];
crc = (char)((data_t&0xFF) ^ (crc));
for(i=0;i<8;i++){
if((crc&0x1)==1){
crc = (char)((crc>>1) ^ (0xa001));
}else{
crc >>= 1;
}
}
}
return (char)((crc>>8)|(crc<<8));
}
}
public class CRC16{
public static void main(String arg[]){
int a[]={0x01,0x03,0x11,0xFF};
char value,i,m;
int j;
MyClass crc=new MyClass();
value = crc.crc16(a,a.length);
System.out.print("( 二进制 )value=");
for(i=0;i<16;i++){
j=((value&(0x8000>>i))!=0)?1:0;
System.out.print(j);
}
System.out.println();
System.out.print("(十六进制)value=");
for(i=3;i>=0;i--){
m=(char)(((value>>(i*4))&(0x000F)));
if((m>=0)&&(m<=9)){
m +='0';
}else if((m>=10)&&(m<=15)){
m=(char)(m-10+'A');
}
System.out.print(m);
if((i%2)==0){
System.out.print(" ");
if(i==0)break;
}
}
}
}
网友评论