All other questions regarding DCMTK
Moderator: Moderator Team
Leonid
Posts: 6 Joined: Thu, 2009-07-09, 09:29
#1
Post
by Leonid » Wed, 2010-06-09, 07:29
Hello!
I have following problem: when I tried to calculate CRC32 for file 1.txt with content "ABCDEFGHIJKLMNOPQRSTUVWXYZ" using OFCRC32::compute method I received unexpected result 0x041BC610. But when I calc CRC32 for this file by another program I had 0xABF77822
Here is my code:
Code: Select all
FILE* f = NULL;
OFCRC32 crc32;
USES_CONVERSION;
const char* fname = OLE2A(filename);
unsigned long res = 0;
f = fopen(fname, "rb");
fseek(f, 0L, SEEK_END);
unsigned long size = ftell(f);
rewind(f);
char *buf = new char[size];
memset(buf, 0, (size_t)size);
fread(buf, 1, size, f);
res = crc32.compute(buf,size); // res = 0x041BC610 instead of 0xABF77822
fclose(f);
delete [] buf;
Please help me. What I'm doing wrong?
Jörg Riesmeier
ICSMED DICOM Services
Posts: 2217 Joined: Fri, 2004-10-29, 21:38
Location: Oldenburg, Germany
#2
Post
by Jörg Riesmeier » Wed, 2010-06-09, 20:39
Are you sure that the other program uses the same algorithm?
Leonid
Posts: 6 Joined: Thu, 2009-07-09, 09:29
#3
Post
by Leonid » Thu, 2010-06-10, 08:04
Jörg Riesmeier wrote: Are you sure that the other program uses the same algorithm?
But CRC32 should have the same value for all algorithms? Or I'm wrong?
Here is algorithm used by another program (crc_tab is the same as in OFCRC32 class) :
Code: Select all
unsigned long update_crc_32( unsigned long crc, char c ) {
unsigned long tmp, long_c;
long_c = 0x000000ffL & (unsigned long) c;
tmp = crc ^ long_c;
crc = (crc >> 8) ^ crc_tab[ tmp & 0xff ];
return crc;
}
sample of usage:
Code: Select all
FILE* f = NULL;
f = fopen(fname, "rb")
crc_32 = 0xffffffffL;
while( ( ch=fgetc( f ) ) != EOF )
{
crc_32 = update_crc_32(crc_32, (char) ch);
}
fclose( fp );
crc_32 ^= 0xffffffffL;
As I can see, algorithm of CRC32 calculation is the same.
Users browsing this forum: Ahrefs [Bot] and 1 guest