Java中的CRC16 DNP校验和算法

Java中的CRC16 DNP校验和算法,java,unsigned,crc,Java,Unsigned,Crc,我一直在尝试从这个实现(从一个受限制的设备)翻译CRC16 DNP,但没有任何运气 uint16_t Crc16Dnp_computeBuffer(uint8_t const (* const message), uint16_t const ui16Size){ uint16_t remainder = INITIAL_REMAINDER; uint16_t data; uint8_t byte; // assert_panic message != 0U /* * Divide the

我一直在尝试从这个实现(从一个受限制的设备)翻译CRC16 DNP,但没有任何运气

uint16_t Crc16Dnp_computeBuffer(uint8_t const (* const message), uint16_t const ui16Size){
uint16_t remainder = INITIAL_REMAINDER;
uint16_t data;
uint8_t byte;

// assert_panic message != 0U

/*
 * Divide the message by the polynomial, a byte at a time.
 */
for (byte = 0; byte < ui16Size; ++byte) {
    data = reflect_byte(message[byte]) ^ (remainder >> (16U - 8U));
    remainder = crcTable[data] ^ (remainder << 8U);
}

/*
 * The final remainder is the CRC.
 */
return (reflect_word(remainder) ^ FINAL_XOR_VALUE);
}
在这种情况下,reflect_xx方法不做任何操作就返回数据。 我一直在尝试翻译这个,但是遇到了Java未签名的问题。。。 我当前的代码:

 public static short crc16dnpComputeBuffer_U(byte[] buf) {
    int remainder = 0x0000;
    int data;
    /*
 * Divide the message by the polynomial, a byte at a time.
     */
    for (int i = 0; i < buf.length; i++) {
        data = (buf[i] ^ (remainder) >>> ((16) - (8))) & 0xffff;
        System.out.println(data);
        remainder = crcTable[data] ^ (remainder << 8) & 0xffff;
    }
    /*
 * The final remainder is the CRC.
     */
    return (short) (remainder ^ (short) 0xFFFF);
}
crc是
E44B

有没有关于我做错了什么的线索

谢谢

更新:在你的帮助下,我终于得到了:

public static short crc16dnpComputeBuffer_U(byte[] buf) {
    int remainder = 0x0000;
    int data;
    /*
 * Divide the message by the polynomial, a byte at a time.
     */
    for (int i = 0; i < buf.length; i++) {
        data = (buf[i] ^ remainder >>> (16) - (8)) & 0xff;
        remainder = crcTable[data] ^ (remainder << 8);
    }
    /*
 * The final remainder is the CRC.
     */
    return (short) (remainder ^ (short) 0xFFFF);
}
公共静态短crc16dnpComputeBuffer_(字节[]buf){
整数余数=0x0000;
int数据;
/*
*将消息除以多项式,每次一个字节。
*/
对于(int i=0;i>>(16)-(8))&0xff;

余数=crcTable[data]^(余数
short
byte
在Java中是符号扩展到
int
,因为所有内容都是有符号的。您需要
&0xff
数据的表达式来获得范围为0..255的值。(这样就不需要内部
&0xfff


还要注意的是,返回值大约有一半的时间是负数,但这实际上是C实现返回的16位值,作为正的无符号值。

试着看一下这里,它在几个月前帮了我很多忙,我建议在使用
^
&
时使用括号>,和
>
运算符,因为它们的优先级不明显。我必须查找Java优先级表,以确定哪一个先应用。感谢这两个注释!
08AA0001B6340020040B1B1DE5000000000000000000287D0100000000003A7D0100000000003D7D010000000000437D0100000000002E7D0100000000003F7D010000000000367D0100000000003C7D0100000000003E7D0100000000002B7D0100000000003B7D010000000000397D010000000000427D010000000000357D0100000000003F7D010000000000317D0100000000003C7D010000000000387D010000000000467D010000000000
public static short crc16dnpComputeBuffer_U(byte[] buf) {
    int remainder = 0x0000;
    int data;
    /*
 * Divide the message by the polynomial, a byte at a time.
     */
    for (int i = 0; i < buf.length; i++) {
        data = (buf[i] ^ remainder >>> (16) - (8)) & 0xff;
        remainder = crcTable[data] ^ (remainder << 8);
    }
    /*
 * The final remainder is the CRC.
     */
    return (short) (remainder ^ (short) 0xFFFF);
}