Search code examples
flutterdartchecksum

Flutter checksum Crc16Xmodem of crclib does not return expected value


In my Flutter project, I tried to get a checksum for the string '00020101021230480016A00000067701011201150105523009350080205012095802TH62200716SCOSM800129099915303764540510.006304' to make a payment QR code. The intended checksum string should be 0x0000AAC1 and should be a string type. I tried below lib but failed to get the result.

crclib: ^3.0.0

Neither of The following two lines could return the expected value.

String cdata='00020101021230480016A00000067701011201150105523009350080205012095802TH62200716SCOSM800129099915303764540510.006304';
Crc16Xmodem().convert(utf8.encode(cdata)).toString(); // returns 46184
Crc16Xmodem().convert(utf8.encode(cdata)).toRadixString(16); // returns b468

Please help me, thank you.

need the desired string value 0x0000AAC1 from Crc16Xmodem


Solution

  • If the desired output is AAC1, you should be using the CRC-16/CCITT-FALSE algorithm, not Crc16Xmodem.

    Remember that you need to pad the output too if you want it on the 0x0000xxxx format.

    String cdata = '00020101021230480016A00000067701011201150105523009350080205012095802TH62200716SCOSM800129099915303764540510.006304';
    final crc = Crc16CcittFalse().convert(utf8.encode(cdata));
    final crcString = '0x${crc.toRadixString(16).padLeft(8, '0')}';
    print(crcString);
    // 0x0000AAC1