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