Created
August 13, 2024 20:31
-
-
Save brunocmorais/bbd008481d428ae093ded9632742daeb to your computer and use it in GitHub Desktop.
Simple Arduino CRC32 calculator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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