英文:
Flutter checksum Crc16Xmodem of crclib does not return expected value
问题
在我的Flutter项目中,我尝试为字符串'00020101021230480016A00000067701011201150105523009350080205012095802TH62200716SCOSM800129099915303764540510.006304'获取校验和,以生成支付二维码。
预期的校验和字符串应为0x0000AAC1,并且应为字符串类型。
我尝试了下面的库但未能获得结果。
crclib: ^3.0.0
以下两行代码都无法返回预期值。
String cdata = '00020101021230480016A00000067701011201150105523009350080205012095802TH62200716SCOSM800129099915303764540510.006304';
Crc16Xmodem().convert(utf8.encode(cdata)).toString(); // 返回46184
Crc16Xmodem().convert(utf8.encode(cdata)).toRadixString(16); // 返回b468
请帮助我,谢谢。
需要从Crc16Xmodem获取期望的字符串值0x0000AAC1。
英文:
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
答案1
得分: 0
如果期望的输出是 AAC1
,你应该使用 CRC-16/CCITT-FALSE
算法,而不是 Crc16Xmodem
。
请记住,如果你想得到 0x0000xxxx
格式的输出,你也需要对输出进行填充。
String cdata = '00020101021230480016A00000067701011201150105523009350080205012095802TH62200716SCOSM800129099915303764540510.006304';
final crc = Crc16CcittFalse().convert(utf8.encode(cdata));
final crcString = '0x${crc.toRadixString(16).padLeft(8, '0')}';
print(crcString);
// 0x0000AAC1
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论