Skip to content

Instantly share code, notes, and snippets.

@WietseWind
Created February 23, 2025 01:49
Show Gist options
  • Save WietseWind/ee21aab356053c61c2887f72d9a244a0 to your computer and use it in GitHub Desktop.
Save WietseWind/ee21aab356053c61c2887f72d9a244a0 to your computer and use it in GitHub Desktop.
CRC16 / CRC-A implementation of ISO 14443-3 in Java
// Eg run @ https://www.programiz.com/java-programming/online-compiler/
// data: 48 65 6C 6C 6F 20 57 6F 72 6C 64
// CRC-16: 52 7B
class Main {
public static byte[] calculateCRC16(byte[] bytes) {
byte chBlock;
int wCRC = 0x6363;
int i = 0;
do {
chBlock = bytes[i++];
chBlock ^= (byte) (wCRC & 0x00FF);
chBlock = (byte) (chBlock ^ (chBlock << 4));
wCRC = ((wCRC >> 8) ^ ((chBlock & 0xFF) << 8) & 0xFFFF) ^
(((chBlock & 0xFF) << 3) & 0xFFFF) ^
(((chBlock & 0xFF) >> 4) & 0xFFFF);
} while (i < bytes.length);
return new byte[] { (byte) (wCRC & 0xFF), (byte) ((wCRC >> 8) & 0xFF) };
}
public static String bytesToHex(byte[] bytes) {
StringBuilder result = new StringBuilder();
for (byte b : bytes) {
result.append(String.format("%02X ", b));
}
return result.toString().trim();
}
public static void main(String[] args) {
byte[] data = "Hello World".getBytes();
System.out.println("data: " + bytesToHex(data));
byte[] crc = calculateCRC16(data);
System.out.println("CRC-16: " + bytesToHex(crc));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment