1,105,578 Community Members

Help Checksum program..

Member Avatar
iamai
Newbie Poster
17 posts since Oct 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
iamai
Newbie Poster
17 posts since Oct 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article