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!

Recommended Answers

All 9 Replies

Post your code! Check your variables are properly set signed/unsigned!

Post your code! Check your variables are properly set signed/unsigned!

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

You may want to try a binary file read.

You may want to try a binary file read.

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

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 ..

I instead used a crc32.c script for use with subprocess .. Faster, and easier ..

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.

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.