V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
cs5117155
V2EX  ›  程序员

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

  •  
  •   cs5117155 · 2021-05-10 17:20:47 +08:00 · 1421 次点击
    这是一个创建于 1295 天前的主题,其中的信息可能已经有所发展或是发生改变。

    以下是 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,是我哪里算错了吗

    4 条回复    2021-05-11 11:58:05 +08:00
    aa65535
        1
    aa65535  
       2021-05-10 17:29:57 +08:00
    明明加起来是 2631,不知道你为什么能得到 2551
    SingeeKing
        2
    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
        3
    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
        4
    cs5117155  
    OP
       2021-05-11 11:58:05 +08:00
    谢谢大家,原来我自已程序过滤了一个字节,导致结果错误了,2631 是对的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1036 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 22:05 · PVG 06:05 · LAX 14:05 · JFK 17:05
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.