반응형
flutter dart language
crc16 checksum
import 'dart:typed_data';
/// crc 16 체크 유틸
///
/// @author 임성진
class CRCCheckUtil {
static Uint16List crcTable = Uint16List(256);
static int polynomial = 0x8005;
static int checkCrc16(List<int> bytes) {
int crc = 0x0000;
for (final byte in bytes) {
crc ^= byte << 8;
for (int i = 0; i < 8; i++) {
if ((crc & 0x8000) != 0) {
crc = (crc << 1) ^ polynomial;
} else {
crc <<= 1;
}
}
}
return crc & 0xFFFF;
}
/// checkSum
/// @param data 검증 데이터
/// @param checkCrc crc
/// @return true : 성공, false 실패
static bool checkSum(Uint8List data, Uint8List checkCrc) {
int crc = checkCrc16(data);
Uint8List bCrc = fnShortToBytes(crc, 1);
if (bCrc[0] == checkCrc[0] && bCrc[1] == checkCrc[1]) {
return true;
} else {
return false;
}
}
static Uint8List fnShortToBytes(int value, int order) {
Uint8List temp;
temp = Uint8List.fromList([(value & 0xFF00) >> 8, value & 0x00FF]);
temp = fnChangeByteOrder(temp, order);
return temp;
}
static Uint8List fnChangeByteOrder(Uint8List value, int Order) {
int idx = value.length;
Uint8List Temp = Uint8List(idx);
if (Order == 1) {
Temp = value;
} else if (Order == 0) {
for (int i = 0; i < idx; i++) {
Temp[i] = value[idx - (i + 1)];
}
}
return Temp;
}
}
반응형
'Programming > Flutter' 카테고리의 다른 글
[Flutter/Window] 사운드 재생(Sound Play) (0) | 2024.05.30 |
---|---|
[Flutter] Http POST (json) (0) | 2024.03.17 |
[Flutter] StreamController(Broadcast, Listener) 구현하기 (0) | 2024.03.09 |
[Flutter] Flutter 시작하기 & 후기 (0) | 2021.03.28 |