Hi everybody
i want write a checksum function using python but i cant ..

#example
#find the checksum of string_data
string_data="0123456789abcdef"
cksum=checksum(string_data)

the checksum will be
sum of 01+23+45+...+ef (adding each byte)
then
next result = ~result
and finally
result = result+1

def checksum(data):
..........
return result

my main problem is the addition
what should i do ??
thanks

So ... here is my code ..
its not the best but at least it works

def checksum(data):
    data = data[1:]
    data_list=[]
    for i in xrange(0,len(data),2):
        data_list.append(data[i:i+2])
    print data_list

    csum="0x00"
    for i in data_list:
        csum=hex(int(csum,16)+int('0x'+i,16))
    csum=hex( int('0x100',16) - int(csum,16) )[2:]
    print csum
    return csum

data=":a01000050000"
data=data + checksum(data)
print data
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.