-
-
Save alextarrago/3c0cad86ee4a6a61f49120960471c0ea to your computer and use it in GitHub Desktop.
CRC8 Algorithm for Dart/Flutter
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
| import 'dart:typed_data'; | |
| class CRC8Utils { | |
| static final CRC8Utils _singleton = new CRC8Utils._internal(); | |
| factory CRC8Utils() { | |
| return _singleton; | |
| } | |
| CRC8Utils._internal(); | |
| static CRC8Utils getInstance() { | |
| createTable(); | |
| return new CRC8Utils(); | |
| } | |
| static var polynomial = 0x07; | |
| static var mask = 0xff; | |
| static var table = Uint8List(256); | |
| static var initialValue = 0x00; | |
| static createTable() { | |
| for (var idx = 0; idx < table.length; idx++) { | |
| var c = idx; | |
| for (var idx2 = 0; idx2 < 8; idx2++) { | |
| if (c & 0x80 != 0) { | |
| c = (((c << 1) & mask) ^ polynomial); | |
| } else { | |
| c <<= 1; | |
| } | |
| } | |
| table[idx] = c; | |
| } | |
| } | |
| int makeCRC8Int(Uint8List list) { | |
| var crc8 = initialValue; | |
| for (var idx = 0; idx < list.length; idx++) { | |
| crc8 = table[crc8 ^ list[idx]]; | |
| } | |
| return crc8; | |
| } | |
| } | |
| // Using | |
| print(CRC8Utils.getInstance().makeCRC8Int(Uint8List.fromList([0x49, 0x4E, 0x46, 0x4F]))); // Result: 88 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment