Skip to content

Instantly share code, notes, and snippets.

@brunocmorais
Created August 13, 2024 20:31
Show Gist options
  • Select an option

  • Save brunocmorais/bbd008481d428ae093ded9632742daeb to your computer and use it in GitHub Desktop.

Select an option

Save brunocmorais/bbd008481d428ae093ded9632742daeb to your computer and use it in GitHub Desktop.
Simple Arduino CRC32 calculator
void setup() {
Serial.begin(9600);
}
void loop() {
while (Serial.available() == 0) {
String text = Serial.readString();
if (text.length() > 0) {
unsigned long crc = crc32(text);
Serial.write("CRC32 '");
Serial.print(text);
Serial.write("': ");
Serial.println(crc, HEX);
}
}
}
unsigned long crc32(String input) {
const unsigned long divisor = 0xEDB88320;
unsigned long crc = 0xFFFFFFFF;
for (int i = 0; i < input.length(); i++) {
crc = crc ^ input[i];
for (int j = 0; j < 8; j++) {
bool odd = (crc & 1) == 1;
crc >>= 1;
if (odd)
crc ^= divisor;
}
}
return crc ^ 0xFFFFFFFF;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment