crc16实现java [英] crc16 implementation java

查看:71
本文介绍了crc16实现java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在计算Java中字节数组的CRC-16实现时遇到问题。基本上,我试图将字节发送到开始写入标签的RFID。通过在Mac上查看tcpdump命令,可以看到数组的校验和值。但是我的目标是自己生成它。这是我的字节数组,应生成0xbe,0xd9:

I am having problems with calculating CRC-16 implementation of a byte array in java. Basically I am trying to send bytes to a RFID that starts writing to a tag. I can see the checksum value of array by looking tcpdump command on mac. But my goal is to generate it by myself. Here is my byte array which should generate 0xbe,0xd9:

byte[] bytes = new byte[]{(byte) 0x55,(byte) 0x08,(byte) 0x68, (byte) 0x14, 
                          (byte) 0x93, (byte) 0x01, (byte) 0x00, (byte) 0x00, 
                          (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x06, 
                          (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x00, 
                          (byte) 0x13, (byte) 0x50, (byte) 0x00, (byte) 0x00, 
                          (byte) 0x00, (byte) 0x22, (byte) 0x09, (byte) 0x11};

0x55是标题。如文档所述,它将被排除在外。

0x55 is the header. As the documentation says it will be excluded.

每当我在java(使用0xbe,0xd9)上尝试此数组时,RFID都可以工作。我的问题是这些校验和值的生成。我搜索了几乎整个网络,但没有机会。我找不到产生0xbe,0xd9的算法。

Whenever I try this array on java (with 0xbe,0xd9), RFID works. My problem is the generating of those checksum values. I searched almost entire web but no chance. I couldn't find any algorithm that produces 0xbe,0xd9.

任何想法对我来说都是最欢迎的。

Any idea is most welcome for me. Thanks in advance.

编辑:这是rfid随附的协议

推荐答案

不是真的确定这是否是Java中C crc16算法的正确翻译....
,但是它显示了示例的正确结果!

I'm not really sure if this is the correct translation in Java of the C crc16 algorithm.... but it shows the correct result for your example!

在使用之前,请先将其他结果与Mac的CRC16进行比较并进行压力测试

public class Crc16 {
public static void main(String... a) {
    byte[] bytes = new byte[] { (byte) 0x08, (byte) 0x68, (byte) 0x14, (byte) 0x93, (byte) 0x01, (byte) 0x00,
            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x06, (byte) 0x00, (byte) 0x00, (byte) 0x01,
            (byte) 0x00, (byte) 0x13, (byte) 0x50, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x22, (byte) 0x09,
            (byte) 0x11 };
    byte[] byteStr = new byte[4];
    Integer crcRes = new Crc16().calculate_crc(bytes);
    System.out.println(Integer.toHexString(crcRes));

    byteStr[0] = (byte) ((crcRes & 0x000000ff));
    byteStr[1] = (byte) ((crcRes & 0x0000ff00) >>> 8);

    System.out.printf("%02X\n%02X", byteStr[0],byteStr[1]);
} 

int calculate_crc(byte[] bytes) {
    int i;
    int crc_value = 0;
    for (int len = 0; len < bytes.length; len++) {
        for (i = 0x80; i != 0; i >>= 1) {
            if ((crc_value & 0x8000) != 0) {
                crc_value = (crc_value << 1) ^ 0x8005;
            } else {
                crc_value = crc_value << 1;
            }
            if ((bytes[len] & i) != 0) {
                crc_value ^= 0x8005;
            }
        }
    }
    return crc_value;
}

}

这篇关于crc16实现java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆