【问题标题】:Need help for checksum on python需要有关 python 校验和的帮助
【发布时间】:2015-10-28 15:50:34
【问题描述】:

我在使用从中国购买的车辆跟踪器时遇到了问题。制造商只向我发送了以下功能。

谁能帮我用python做这个校验和:

Checksum (2 byte)*

Unsigned char Checksum (const char *s,int Length)
{
    Unsigned char result;

    result = 0;

    for(int i=0;i<Length;i++)
    {
        result ^=*s++
    }

    return result;
}

更新。我从接受的答案中得到了它与下面的代码一起使用。

chk = reduce(lambda a,b : a^b, [ord(c) for c in s])

【问题讨论】:

  • 您问题中的代码不是有效的 C/C++(假设它应该是这样)。
  • 尝试在 python 中添加你的代码 sn-p - 展示了努力。
  • 这段代码是我在网上买的 GPS 追踪器附带的。这是发送GPRS命令..

标签: python python-2.7


【解决方案1】:

给你:

s = "test"
chk = reduce(lambda a,b : a^b, [ord(c) for c in s])

print "Checksum : ", chk, " \n"

sh-4.3$ python main.py                                                                                         
Checksum :  22  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-04
    • 1970-01-01
    • 2015-06-23
    • 2014-12-26
    • 2013-08-12
    • 2014-06-11
    相关资源
    最近更新 更多