CRC calculation in Java

别来无恙 提交于 2019-12-13 15:22:29

问题


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?


回答1:


Try using Jacksum.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/732896/crc-calculation-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!