Java Modbus CRC16 Checksum

Source: Internet
Author: User

Recently in a hardware communication with the front-end middleware, in which data validation using CRC16, online search for a lot of information, but found that the final calculation results are different ... It took two days to finally settle down and make a record.

1/** 2 * Calculate CRC16 Check Code 3 * 4 * @param bytes byte array 5 * @return {@link String} check Code 6 * @since 1.0 7 */8 public static String GETCRC (byte[] bytes) {9 int CRC = 0X0000FFFF; Polynomial int = 0x0000a001; int I, j;12 for (i = 0; i < bytes.length; i++ ) {CRC ^= ((int) bytes[i] & 0X000000FF ); + for (j = 0; J < 8; J + + ) {[CRC & 0x00000001]! = 0 ) {CRC >>= 1 ; CRC ^=  polynomial;18} else  {>>= 1 ; }21 }22 }23 return  Integer.tohex String (CRC), }25 26/**27 * Convert 16-single-precision floating-point type to 10-input floating-point type *29 * @return Float30 * @since 1.031 */32 Private float  Parsehex2float (String hexstr) {BigInteger BigInteger = new BigInteger (hexstr, + ); return  Float.intbits Tofloat (Biginteger.intvalue ()); }36 37/**38 * Convert decimal floating-point type to hexadecimal floating-point type *40 * @return String41 * @since 1.042 */43 pri Vate String Parsefloat2hex (float  data) {integer.tohexstring return  (float.floattointbits (data)); 45}  

Java Modbus CRC16 Checksum

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.