确定 8 位模 256 校验和形式的 ascii 字符串 [-Python]

fro*_*ger 3 python hex ascii checksum modulo

我想确定 ASCII 字符串的 8 位模 256 校验和。我知道公式是:

checksum = (sum of bytes)%256

我怎样才能在Python中做到这一点(操作字节)?如果我以字符串“1c03e8”开头,我应该输出0x94. 主要问题是我不确定如何找到 ASCII 字符串的字节总和。这是我正在寻找的主要想法:

https://www.scadacore.com/tools/programming-calculators/online-checksum-calculator/

它有 CheckSum8 Modulo 256

我努力了:

component = ('1c03e8') 
for i in range(len(component)):
            checksum.append(int(float(component[i].encode("hex"))))
            print checksum
    print hex(int(sum(checksum)%256))
Run Code Online (Sandbox Code Playgroud)

这虽然给了我 0x52

rob*_*bro 5

您需要将字符串编码为 ASCII,因为正如您所说,它是一个 ASCII 字符串。

示例,快速而肮脏的解决方案:

print(hex(sum('1c03e8'.encode('ascii')) % 256))
Run Code Online (Sandbox Code Playgroud)