I need to convert this C code to python.
void GetChkSum(Int len, PSTR buf, PSTR res)
{
memset(res, 0, 4);
for(int i=0; i<len; i+=4)
{
res[0]^=(buf+i)[0];
res[1]^=(buf+i)[1];
res[2]^=(buf+i)[2];
res[3]^=(buf+i)[3];
}
res[0]=~res[0];
res[1]=~res[1];
res[2]=~res[2];
res[3]=~res[3];
}
In my case, buf is bytes object.
for example: buf = b'00000001TXBEG 0000'
len is its length: 18
I've already tried convert it, below is my code. I got this error:
'bytes' object does not support item assignment.
def checkSum(xLen, buff) :
res = b'0000'
i = 0
while(i < xLen) :
res[0] ^= buff[i]
res[1] ^= buff[i+1]
res[2] ^= buff[i+2]
res[3] ^= buff[i+3]
i += 4
res[0] = not res[0]
res[1] = not res[1]
res[2] = not res[2]
res[3] = not res[3]
return res
Any suggestions appreciated.
Thanks in advance.