You really need to give an example input and what you expect as output. Any code that uses a recent version of Python, extracts an integer in range(256) from each byte, sums those integers, and finally does total &= 0xFFFFFFFF
should do the job (assuming that your unsigned long
is 32 bits wide).
Note that the last step ( the &=
) is pointless if your file is less than about 16MB in size ... it won't overflow; 16843009 * 255 <= 0xFFFFFFFF < (16843009 + 1) * 255
That means that if your test file is smaller than 16843010 bytes, you must have a problem in your C code or your Python code or both.
You said that "of course" this code:
f=open("file_to_sum",'rb')
m = f.read()
f.close()
sum( array.array('B', m) )
"doesn't work". Does it work if you replace the last line by
print sum( array.array('B', m) )
?
If none of the above is of any help, and you want sensible answers instead of guesses, provide example input, expected output, C code, C output, Python code, Python output. Both the C code and the Python code should be standalone-runnable, and should include printing the size of the byte array being summed.