Create CRC16.java (#3733)

* Create CRC16.java

* Create CRC16Test.java
This commit is contained in:
rnzit
2022-11-09 11:50:54 +07:00
committed by GitHub
parent eb375a6015
commit b8d6b1a9b0
2 changed files with 52 additions and 0 deletions

View File

@ -0,0 +1,29 @@
package com.thealgorithms.others;
/**
* Generates a crc16 checksum for a given string
*/
public class CRC16 {
public static void main(String[] args) {
System.out.println(crc16("Hello World!"));
}
public static String crc16(String message) {
int crc = 0xFFFF; // initial value
int polynomial = 0x1021; // 0001 0000 0010 0001 (0, 5, 12)
byte[] bytes = message.getBytes();
for (byte b : bytes) {
for (int i = 0; i < 8; i++) {
boolean bit = ((b >> (7 - i) & 1) == 1);
boolean c15 = ((crc >> 15 & 1) == 1);
crc <<= 1;
if (c15 ^ bit)
crc ^= polynomial;
}
}
crc &= 0xffff;
return Integer.toHexString(crc).toUpperCase();
}
}