I wrote 2 funcs that work good, but I want to change part of my code to make it more efficient, using for loop with “jump” of every 8 charts.
when I run verify_checksum, I get:
AttributeError: 'int' object has no attribute 'split'
but when I run it with lines remarks with # it works fine.
any idea how fix it without change other parts of the code? (there are more funcs that works with these, and its cause a mess).
my code:
def xor_bytes(byte1, byte2):
byte1, byte2=byte1.split(), byte2.split()
xor=""
a=0
for i in byte1:
for j in i:
t=int(byte2[0][a])^int(j)
xor+="".join(str(t))
a+=1
return xor
def verify_checksum(datagram):
datagram=list(datagram)
org_checksum=datagram[48:56]
org_checksum="".join(org_checksum)
x=48
for i in datagram[48:56]:
datagram[x]='0'
x+=1
datagram="".join(datagram)
res=xor_bytes(datagram[0:8], datagram[8:16])
for i in (16,88,8):
res=xor_bytes(res, i)
#res=xor_bytes(res,datagram[16:24])
#res=xor_bytes(res,datagram[24:32])
#res=xor_bytes(res,datagram[32:40])
#res=xor_bytes(res,datagram[40:48])
#res=xor_bytes(res,datagram[48:56])
#res=xor_bytes(res,datagram[56:64])
#res=xor_bytes(res,datagram[64:72])
#res=xor_bytes(res,datagram[72:80])
#res=xor_bytes(res,datagram[80:88])
if res==org_checksum:
return True
else:
return False
input:
verify_checksum("1111000000001111000011111111000001010101101010101010111001110011001000000110101101101001")
output:
True
In the lines that are commented out, you’re passing two strings as parameters.
In the loop, you’re passing a string and an int as parameters.
The error is on the byte2.split() since it’s an int. Pass in a section of the datagram, not a numeric position and you’ll be fine.