jenlu
jenlu

Reputation: 49

How to generate checksum from hex byte using python

I am creating a hex file using python and at the end I need to add a checksum that consists of sum of all hex values so that checksum = Byte 0x000000 + Byte 0x000001 + … + Byte 0x27DAFF (not including this 4 bytes). This checksum shall then be written to buffer at position 0x27DB00-0x27DB03 as unsigned long.

Any good ideas for how to get this done fast, I am running python2.7.

As info of what I am up to I start with creating a buffer using ctypes, then write lots and lots of hex stuff to buffer, then create a cStringIO from buffer and write this string object to a file_obj which happen to be a django http response (i.e. returns the hex file as downloadable file) so any smart things involving the buffer would be appreciated

Upvotes: 0

Views: 7748

Answers (1)

jenlu
jenlu

Reputation: 49

the following two solutions worked:

checksum = sum(map(ord, b))

or:

checksum = sum(bytearray(b))

Upvotes: 4

Related Questions