We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,716 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Help Checksum program..

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

1
Contributor
1
Reply
6 Hours
Discussion Span
3 Years Ago
Last Updated
2
Views
iamai
Newbie Poster
17 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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
iamai
Newbie Poster
17 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0544 seconds using 2.65MB