Please indicate the original source, thank you!
instructions
RocketMQ(2) : RPC communication RocketMQ(2) : RPC communication RocketMQ(2) : RPC communication RocketMQ(2) : RPC communication RocketMQ(2) : RPC communication RocketMQ(2) : RPC communication
Introduction of CRC
The CRC is the most common error check code in the data communication field. The information field and the length of the check field can be arbitrarily selected. Cyclic redundancy check (CRC) is a data transmission error detection function that performs polynomial calculations on the data and appends the resulting result to the frame. The receiving device performs similar algorithms to ensure correctness and integrity of the data transmission.
CRC32 has strong error detection ability, low overhead, and is easy to be realized by encoder and detection circuit. From the point of view of its error detection ability, the probability that it can not find the error is less than 0.0047%. In terms of performance and cost, it is far superior to parity check and arithmetic and check. CRC is therefore ubiquitous in data storage and data communication: Crc-ccitt is used in FCS (Frame error detection sequence) of famous communication protocol X.25, CRC32 is used in ARJ, LHA and other compression software, CRC16 is used in reading and writing of disk drives, and CRC is also used in common image storage formats such as GIF and TIFF.
CRC using
public static void main(String[] args) {
int crc = crc32("Welcome to: Ingenuity Zero.".getBytes());
int crc1 = crc32("Welcome to: Ingenuity Zero.".getBytes());
int crc2 = crc32("Welcome to: Ingenuity Zero!".getBytes());
System.out.println(crc==crc1);
System.out.println(crc== crc2);
}
public static int crc32(byte[] array) {
if(array ! =null) {
return crc32(array, 0, array.length);
}
return 0;
}
public static int crc32(byte[] array, int offset, int length) {
CRC32 crc32 = new CRC32();
crc32.update(array, offset, length);
return (int) (crc32.getValue() & 0x7FFFFFFF);
}
Copy the code
Output result:
true
false
Copy the code
Rocketmq is partially used
Crc32 JDK internal implementation
Through repeated shifts and CRC division, the final value in the register is the remainder we want.
Note: the specific implementation details did not go to understand, need to understand please learn further.
If you feel that you have gained something after reading it, please click “like”, “follow” and add the official account “Ingenuity Zero” to check out more wonderful history!!
Join knowledge Planet and discuss together!