开发者

How to generate checksum from hex byte using python

开发者 https://www.devze.com 2023-03-24 12:34 出处:网络
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 includin

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 stri开发者_如何学Cng 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


the following two solutions worked:

checksum = sum(map(ord, b))

or:

checksum = sum(bytearray(b))
0

精彩评论

暂无评论...
验证码 换一张
取 消