DelphiFAQ Home Search:

Are 2 files identical? Do a 32bit CRC sum..

 

comments3 comments. Current rating: 4 stars (2 votes). Leave comments and/ or rate it.

To determine, whether 2 files are identical, I first would compare their sizes.

If they are identical, I would calculate a 32bit CRC sum for each of them and compare the results.
It is extremely unlikely that the files are different but have the same CRC.

Someone asked me:
"Are you sure? Take for example two files, where the only difference would
be that in one of them, I turned a "hello" into a "ehllo". I guess the checksum
would be the same, don't you think?"

Answer: No, a CRC might even tell you where the difference was (if the files are really that short).
The following function does compute such a 32bit CRC (cyclic redundancy check) value:


    function ComputeFileCRC32(const FileName : string): LongInt;

Since the polynome table is rather long, you may download the pascal file: crc32.zip (2.2kB).
To compare, use something like this:

function Ident (const fn1, fn2 : string) : boolean;
begin
  Ident := ComputeFileCRC32(fn1) = ComputeFileCRC32(fn2);
end;

Content-type: text/html

Comments:

2006-12-16, 02:48:30
anonymous from China  
Thanks !
2007-09-19, 13:21:46
danielcalegari@yahoo.com from Brazil  
rating
why not compare bytes/chars?
this would be faster than calculating 2 checksums!

yours, Daniel
from Brazil
2008-04-03, 10:15:05
Rateb from Syrian Arab Republic  
rating
is this method can be used as identification for a file
this file must be the a certain file?
thanks alot

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: