How to calculate the checksum of this data?

709 Views Asked by At

I have the following data frame:

AA3200010013000CAAAA000000000000000000043000010156EF79

"EF 79" is supposed to be the checksum. but I am not very sure how to get this value.

in the documentation it says:

Checksum: data CRC16 checksum except for frame head. The calibration algorithm adopts CCITT-16, calibration polynomials is X16 + X15 + X2 + 1, initiation value is set as 0.
2

There are 2 best solutions below

6
Mark Adler On BEST ANSWER

If you drop the leading aa, then this CRC-16 of the remainder:

width=16  poly=0x8005  init=0x0000  refin=false  refout=false  xorout=0x0000  check=0xfee8  residue=0x0000  name="CRC-16/UMTS"

gives ef79.

0
Manas On

Here's how I implemented the checksum calculation:

int crc16umts(List<int> bytes) {
  int crc = 0x0000;
  const int polynomial = 0x8005;
  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;
}

void main() {
  final data = '3200010013000CAAAA000000000000000000043000010156';
  final bytes = List<int>.generate(
      data.length ~/ 2, (i) => int.parse(data.substring(i * 2, i * 2 + 2), radix: 16));
  final checksum = crc16umts(bytes);
  print(checksum.toRadixString(16).toUpperCase()); // EF79
}