DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Python (http://www.daniweb.com/forums/forum114.html)
-   -   Calculate CRC32 checksums (like SFVs) (http://www.daniweb.com/forums/thread201713.html)

dezza Jul 4th, 2009 8:00 pm
Calculate CRC32 checksums (like SFVs)
 
So far i've got this and it returns something like:

-2087276233
import binascii
def getCRC(filename):
    filedata = open(filename).read()
    return binascii.crc32(filedata)

Someone told me that I had to use struct.pack or struct.unpack to change it to hex values.

I am not sure which type to convert to and would like some explanation to understand fully instead of coding in blind.

The final result should show the same CRC32 value as if you packed the file in WinRAR, checked it with the numerous SFV-checkers or whatever.

A link just in case:
http://docs.python.org/library/struct.html

Thanks alot in advance!

wildgoose Jul 5th, 2009 2:15 am
Re: Calculate CRC32 checksums (like SFVs)
 
Post your code! Check your variables are properly set signed/unsigned!

dezza Jul 5th, 2009 9:11 am
Re: Calculate CRC32 checksums (like SFVs)
 
Quote:

Originally Posted by wildgoose (Post 908609)
Post your code! Check your variables are properly set signed/unsigned!

import binascii
def getCRC(filename):
    filedata = open(filename).read()
    return binascii.crc32(filedata)

vegaseat Jul 8th, 2009 8:36 pm
Re: Calculate CRC32 checksums (like SFVs)
 
You may want to try a binary file read.

dezza Jul 8th, 2009 9:11 pm
Re: Calculate CRC32 checksums (like SFVs)
 
Quote:

Originally Posted by vegaseat (Post 912208)
You may want to try a binary file read.

Can you define this method? I am not sure I understand what you mean.

dezza Jul 18th, 2009 2:36 pm
Re: Calculate CRC32 checksums (like SFVs)
 
http://dev.jmoiron.net/code/html/cksum.py.html

Can I use this code example to write a more simple approach?

I would prefer it in a single function, but the classes and etc. confuses me, I think he should simplify his code, it also gives a deprecation warning ..

dezza Jul 23rd, 2009 1:32 am
Re: Calculate CRC32 checksums (like SFVs)
 
I instead used a crc32.c script for use with subprocess .. Faster, and easier ..

jlm699 Jul 23rd, 2009 9:33 am
Re: Calculate CRC32 checksums (like SFVs)
 
Quote:

Originally Posted by dezza (Post 912231)
Can you define this method? I am not sure I understand what you mean.

Binary read is simply opening the file under the mode 'read' plus 'binary'. Read up on the built-in
open
method here, and the answer will reveal itself.

dezza Jul 24th, 2009 11:14 am
Re: Calculate CRC32 checksums (like SFVs)
 
I have tried both open(filename, 'r') and open(filename, 'rb')

I can get the file loaded into a string and then try
print binascii.crc32(file-in-string)

Still just gives me negative integers, what I want is the 8-char hexvalue.

jlm699 Jul 25th, 2009 10:34 am
Re: Calculate CRC32 checksums (like SFVs)
 
Quote:

Originally Posted by dezza (Post 928601)
I have tried both open(filename, 'r') and open(filename, 'rb')

I can get the file loaded into a string and then try
print binascii.crc32(file-in-string)

Still just gives me negative integers, what I want is the 8-char hexvalue.

Use 'rb', and RTFM

Use string formatting to print the hex value, just as the example in the docs shows you.


All times are GMT -4. The time now is 9:15 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC