二进制数据累加求和,取低八位

2021-05-10 17:20:47 +08:00
 cs5117155

以下是 16 进制表示,我把每一个 16 进制转换成 10 进制,然后进行累加,得到 2551

echo 2551&0xff;

转换成 16 进制是f7

5a 54 08 1f 01 01 61 00 50 53 0a 09 4e 01 64 00 00 00 00 00 00 00 00 00 00 00 00 02 00 00 00 00 00 00 00 00 50 53 0a 09 4f 03 64 00 00 00 00 00 00 00 50 53 0a 09 46 04 64 00 00 00 00 00 00 00 50 53 0a 0a 09 05 64 00 00 00 00 00 00 00 50 53 0a 0a 08 06 64 00 00 00 00 00 00 00 50 53 0a 09 34 07 64 00 00 00 00 00 00 00 50 53 0a 0a 0a 08 64 00 00 00 00 00 00 00

但是硬件那边传过来的求和校验是0x47,是我哪里算错了吗

1439 次点击
所在节点    程序员
4 条回复
aa65535
2021-05-10 17:29:57 +08:00
明明加起来是 2631,不知道你为什么能得到 2551
SingeeKing
2021-05-10 17:40:15 +08:00
>>> sum([int(x, 16) for x in '''5a 54 08 1f 01 01 61 00 50 53 0a 09 4e 01 64 00 00 00 00 00 00 00 00 00 00 00 00 02 00 00 00 00 00 00 00 00 50 53 0a 09 4f 03 64 00 00 00 00 00 00 00 50 53 0a 09 46 04 64 00 00 00 00 00 00 00 50 53 0a 0a 09 05 64 00 00 00 00 00 00 00 50 53 0a 0a 08 06 64 00 00 00 00 00 00 00 50 53 0a 09 34 07 64 00 00 00 00 00 00 00 50 53 0a 0a 0a 08 64 00 00 00 00 00 00 00'''.strip().split()])
2631
takitooru
2021-05-11 00:00:38 +08:00
uint8_t test_list[]={0x5a,0x54,0x08,0x1f,0x01,0x01,0x61,0x00,0x50,0x53,0x0a,0x09,0x4e,0x01,0x64,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x50,0x53,0x0a,0x09,0x4f,0x03,0x64,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x50,0x53,0x0a,0x09,0x46,0x04,0x64,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x50,0x53,0x0a,0x0a,0x09,0x05,0x64,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x50,0x53,0x0a,0x0a,0x08,0x06,0x64,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x50,0x53,0x0a,0x09,0x34,0x07,0x64,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x50,0x53,0x0a,0x0a,0x0a,0x08,0x64,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
int main(int argc,char *argv[])
{
uint8_t rst = 0;
for(int i=0;i<sizeof(test_list);i++)
{
rst += test_list[i];
}
printf("%d 0x%02x",rst,rst);
}
gcc .\test6_sig.c -m32
.\a.exe
-> 71 0x47
cs5117155
2021-05-11 11:58:05 +08:00
谢谢大家,原来我自已程序过滤了一个字节,导致结果错误了,2631 是对的

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/776060

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX