CRC calculation in Java

2019-07-13 04:05发布

I'm reading a file from serialport using x-modem protocol and 133 bytes packet. I'm reading in that

      1 byte is SOH
      2 byte packet number
      3 byte nagative of packet number
next  128 bytes data
      2 bytes CRC sent from other side.

I have to calculate CRC of 128 bytes data and 2 bytes crc sent from other side that I have to make it single byte and have to comapare with my calculated crc. How can I do this in java?

3条回答
祖国的老花朵
2楼-- · 2019-07-13 04:18

Try using Jacksum.

查看更多
3楼-- · 2019-07-13 04:26

Sun JDK 1.6 contains sun.misc.CRC16, but there is a possibility this is not the CRC16 you're looking for, since there's several different polynomials in use.

查看更多
Emotional °昔
4楼-- · 2019-07-13 04:26

Here is my C code, which is trivial to port to Java - you are free to use it in any way you like. The references to word are for a 16 bit unsigned value - you should be able to use a char instead in Java.

It's been too long since I worked with 16 bit CRC's so I don't recall if there are variations based on seeding. I am pretty sure I used this code in a C implementation of X-Modem way back when.

The source is posted on tech.dolhub.com.

查看更多
登录 后发表回答