943,788 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 4282
  • Python RSS
Jul 4th, 2009
0

Calculate CRC32 checksums (like SFVs)

Expand Post »
So far i've got this and it returns something like:

-2087276233
Python Syntax (Toggle Plain Text)
  1. import binascii
  2. def getCRC(filename):
  3. filedata = open(filename).read()
  4. 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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dezza is offline Offline
6 posts
since Jul 2009
Jul 5th, 2009
0

Re: Calculate CRC32 checksums (like SFVs)

Post your code! Check your variables are properly set signed/unsigned!
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jul 5th, 2009
0

Re: Calculate CRC32 checksums (like SFVs)

Click to Expand / Collapse  Quote originally posted by wildgoose ...
Post your code! Check your variables are properly set signed/unsigned!
python Syntax (Toggle Plain Text)
  1. import binascii
  2. def getCRC(filename):
  3. filedata = open(filename).read()
  4. return binascii.crc32(filedata)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dezza is offline Offline
6 posts
since Jul 2009
Jul 8th, 2009
0

Re: Calculate CRC32 checksums (like SFVs)

You may want to try a binary file read.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jul 8th, 2009
0

Re: Calculate CRC32 checksums (like SFVs)

Click to Expand / Collapse  Quote originally posted by vegaseat ...
You may want to try a binary file read.
Can you define this method? I am not sure I understand what you mean.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dezza is offline Offline
6 posts
since Jul 2009
Jul 18th, 2009
0

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 ..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dezza is offline Offline
6 posts
since Jul 2009
Jul 23rd, 2009
0

Re: Calculate CRC32 checksums (like SFVs)

I instead used a crc32.c script for use with subprocess .. Faster, and easier ..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dezza is offline Offline
6 posts
since Jul 2009
Jul 23rd, 2009
0

Re: Calculate CRC32 checksums (like SFVs)

Click to Expand / Collapse  Quote originally posted by dezza ...
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.
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Jul 24th, 2009
0

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dezza is offline Offline
6 posts
since Jul 2009
Jul 25th, 2009
0

Re: Calculate CRC32 checksums (like SFVs)

Click to Expand / Collapse  Quote originally posted by dezza ...
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.
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Hello
Next Thread in Python Forum Timeline: How to perform a certain task using subprocess module?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC