浏览 938 次
|
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
|---|---|
| 作者 | 正文 |
|
最后更新时间:2007-11-18 关键字: javaCRC
希望能和大家交流下 CRC-8 的java编程实现
CRC程序的编码 1. 循环冗余编码(Cyclic Redundancy Code, CRC)是目前应用最广的检错码方法之一,它具有检错能力强与实现起来容易的特点。 2. CRC检错方法的工作原理:将要发送的数据比特序列当作一个多项式f(x)的系数,在发送端用收发双方预先约定的生成多项式G(x)去除,求得一个余数多项式。将余数多项式加到数据多项式之后发送到接收端。在接收端用同样的生成多项式G(x)去除接收数据多项式f(x),得到计算余数多项式。如果计算余数多项式与接收余数多项式相同,则表示传输无差错,否则表示传输有差错,由发送方重发数据,直至正确为止。 运行界面: 声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
最后更新时间:2007-12-01
package cn.work.network.crc;
public class CRC8 {
public static int hexToInt(String hex){ //16进制转为10进制
int i = Integer.parseInt(hex,16);
return i;
}
public static String hexToBinaryString(String hex){//16进制转为2进制
int i = Integer.parseInt(hex,16);
return Integer.toBinaryString(i);
}
public static String binTohex(String bin){ //2进制转为16进制
int i = Integer.parseInt(bin,2);
return Integer.toHexString(i);
}
public static int getCRCChecker10(String data,String hex){ //取得检验和(10进制)
int check10=0;
String s = hexToBinaryString(hex);
for (int i = 0;i< s.length()-1;i++){ //根据多项式最高次幂加多少个0
data+="0";
}
int a = Integer.valueOf(data,2);
int b = hexToInt(hex);
check10 = a%b; //求余运算,得到校验和
return check10;
}
public static String getCRCChecker16(String data,String hex){ //取得检验和(16进制)
int check16 = getCRCChecker10(data,hex);
return Integer.toHexString(check16);
}
public static String getCRCChecker2(String data,String hex){ //取得检验和(2进制)
int check2 = getCRCChecker10(data,hex);
return Integer.toBinaryString(check2);
}
public static String getSendData(String data ,String hex ){//取得发送的数据 元数据(二进制)+二进制校验和
String send =data;
send += getCRCChecker2(data,hex);
return send;
}
public static String getXOR(String data1,String data2){ //将2个二进制异或,返回一个二进制数
String XORString ="";
int a = Integer.valueOf(data1,2);
int b = Integer.valueOf(data2,2);
int xor = a^b;
XORString=Integer.toBinaryString(xor);
return XORString;
}
public static String getHexXORBin2(String hex,String bin){ //将1个16进制数和1个二进制异或,返回一个二进制数
String XORString ="";
int a = hexToInt( hex);
int b = Integer.valueOf(bin,2);
int xor = a^b;
XORString=Integer.toBinaryString(xor);
return XORString;
}
public static String getHexXORBin(String hex,String bin){ //将1个16进制数和1个二进制异或,返回一个16进制数
String XORString ="";
int a = hexToInt( hex);
int b = Integer.valueOf(bin,2);
int xor = a^b;
XORString=binTohex(Integer.toBinaryString(xor));
return XORString;
}
public static boolean Check1(String receiver ,String CRCCheck){ //判断接收的数据是否正确,接收的数据%校验和 是否整除
boolean flag = false;
int rec = Integer.valueOf(receiver,2);
int ch = Integer.valueOf(CRCCheck,2);
if ((rec %ch)==0)
flag =true;
return flag;
}
public static boolean Check (String receiver ,String gxhex){ //判断接收的数据是否正确,接收的数据% G(x) 是否整除
boolean flag = false;
int rec = Integer.valueOf(receiver,2);
//System.out.println(rec);
int gx = hexToInt(gxhex);
//System.out.println(gx);
if ((rec %gx)==0)
flag =true;
return flag;
}
public static String getMod (String bin ,String hex){ //将一个2进制数和一个16进制数 取模运算 返回1个2进制序列
int rec = Integer.valueOf(bin,2);
int gx = hexToInt(hex);
int mod= rec %gx;
String s = Integer.toBinaryString(mod);
return s;
}
public static void main (String [] args){
boolean flag = Check("1010011110010010","107");
String s= getMod("1010011110010010","107");
System.out.println(s);
System.out.println(flag);
}
}
|
|
| 返回顶楼 | |
|
最后更新时间:2007-12-01
import java.applet.Applet;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventObject;
public class crc8Appl extends Applet
implements ActionListener
{
private Button btnOk;
private Button btnClear;
private TextField txtMessage;
private TextArea display;
private Label lblFcs;
protected int Fcs;
private Canvas c;
public void init()
{
Panel panel = new Panel();
panel.setLayout(new GridLayout(5, 2, 0, 2));
panel.add(new Label("CRC-8 polynomial, C(x) = "));
panel.add(new Label("x^8 + x^2 + x + 1"));
panel.add(new Label("Enter hex-coded message.."));
panel.add(txtMessage);
panel.add(new Label("Frame Check Sequence....."));
panel.add(lblFcs = new Label("0x00"));
panel.add(new Label("Press OK to calculate."));
panel.add(new Label("Press Clear to reset FCS to 0x00"));
setLayout(null);
c = new Canvas();
add(panel);
add(btnOk);
add(btnClear);
display.setEditable(false);
add(display);
panel.setBounds(6, 6, 400, 110);
btnOk.setBounds(50, 112, 50, 24);
btnClear.setBounds(120, 112, 50, 24);
display.setBounds(200, 112, 210, 100);
txtMessage.addActionListener(this);
btnOk.addActionListener(this);
btnClear.addActionListener(this);
}
public void actionPerformed(ActionEvent actionevent)
{
boolean flag = false;
String s = "00";
if (actionevent.getSource() == btnClear)
{
txtMessage.setText("");
display.setText("");
lblFcs.setText("Enter a hex-coded message..");
Fcs = 0;
return;
}
String s1 = txtMessage.getText().trim();
if (s1 != null)
{
for (int j = 0; j < s1.length() / 2; j++)
{
String s2 = s1.substring(2 * j, 2 * j + 2);
int i = iByte(s2);
if (i >= 0)
{
calculateCRC(i);
s = makeEven(Integer.toString(Fcs, 16));
displayMessage(s2, s);
} else
{
lblFcs.setText("Bad byte in the message....");
return;
}
}
}
lblFcs.setText("Frame Check = 0x" + s);
}
void displayMessage(String s, String s1)
{
display.append("Byte = 0x" + s + " Check = 0x" + s1 + ".\n");
}
public String makeEven(String s)
{
if (s.length() % 2 == 0)
return s;
else
return "0" + s;
}
public int iByte(String s)
{
try
{
return Integer.parseInt(s, 16);
}
catch (NumberFormatException )
{
Fcs = 0;
}
return -1;
}
public void calculateCRC(int i)
{
for (int k = 7; k >= 0; k--)
{
int j = Fcs >> 7 ^ i >> k & 1;
Fcs = Fcs << 1 & 0xff;
for (int l = 1; l <= 3; l++)
{
Fcs ^= j;
j <<= 1;
}
}
}
public static void main(String args[])
{
Frame frame = new Frame();
crc8Appl crc8appl = new crc8Appl();
crc8appl.init();
frame.setSize(450, 240);
frame.setTitle("CRC-8 Calculator");
frame.add("Center", crc8appl);
frame.setVisible(true);
}
public crc8Appl()
{
btnOk = new Button(" OK ");
btnClear = new Button("Clear");
txtMessage = new TextField("", 30);
display = new TextArea("Results window.\n", 5, 30);
}
}
|
|
| 返回顶楼 | |


