|
April 2008
Copy and paste this simplified source code into your project if you
need to generate CRC-32 error detection. If you wish, download the
demonstration programs and source code from the pages listed below. The programs are AppWizard generated dialogs written with MS Visual C++ version 6.0.
Free Utilities at this Website:
32 Bit CRC File Calculation program and C++ source code.
32 Bit CRC Text Calculation program and C++ source code.
Note: Click on the ClipTrak link at the bottom of this page for another free program and source code that makes extensive use of CRC-32.
Aditional information:
How to Verify CRC-32 Numbers
Header File
// CRCdemo.h
protected:
ULONG crc32_table[256]; //
Lookup table array
void Init_CRC32_Table();
// Builds lookup table array
ULONG Reflect(ULONG ref, char
ch); // Reflects CRC bits in the lookup table
int Get_CRC(CString&
text); // Creates a CRC from a text string
Source File
// CRCdemo.cpp
void CRCdemo::Init_CRC32_Table()
{// Call this function only once to initialize
the CRC table.
// This is the
official polynomial used by CRC-32
// in PKZip, WinZip
and Ethernet.
ULONG ulPolynomial = 0x04c11db7;
// 256 values representing
ASCII character codes.
for(int
i = 0; i <= 0xFF; i++)
{
crc32_table[i]=Reflect(i, 8) << 24;
for (int j = 0; j < 8; j++)
crc32_table[i] = (crc32_table[i] << 1) ^ (crc32_table[i] & (1
<< 31) ? ulPolynomial : 0);
crc32_table[i] = Reflect(crc32_table[i], 32);
}
}
ULONG CRCdemo::Reflect(ULONG ref, char
ch)
{// Used only by Init_CRC32_Table().
ULONG value(0);
// Swap bit 0 for
bit 7
// bit 1 for bit
6, etc.
for(int
i = 1; i < (ch + 1); i++)
{
if(ref & 1)
value |= 1 << (ch - i);
ref >>= 1;
}
return
value;
}
int CRCdemo::Get_CRC(CString& text)
{
// Pass a text string to this function and it
will return the CRC.
// Once the lookup table has been filled in by
the two functions above,
// this function creates all CRCs using only
the lookup table.
// Note that CString is an MFC class.
// If you don't have MFC, use the function below
instead.
// Be sure to use
unsigned variables,
// because negative
values introduce high bits
// where zero
bits are required.
// Start out with
all bits set high.
ULONG ulCRC(0xffffffff);
int len;
unsigned char*
buffer;
// Get the length.
len = text.GetLength();
// Save the text
in the buffer.
buffer = (unsigned
char*)(LPCTSTR)text;
// Perform the
algorithm on each character
// in the string,
using the lookup table values.
while(len--)
ulCRC = (ulCRC >> 8) ^ crc32_table[(ulCRC & 0xFF) ^ *buffer++];
// Exclusive OR
the result with the beginning value.
return
ulCRC ^ 0xffffffff;
}
If you don't have an MFC compiler, you can substitute this function,
which doesn't use a CString. Just change the declaration in the header
file to: int Get_CRC(char*
text); // Creates a CRC from a text string
int CRCdemo::Get_CRC(char*
text)
{// Pass a text string to this function and it
will return the CRC.
// Once the lookup table has been filled in by
the two functions above,
// this function creates all CRCs using only
the lookup table.
// Be sure to use
unsigned variables,
// because negative
values introduce high bits
// where zero
bits are required.
// Start out with
all bits set high.
ULONG ulCRC(0xffffffff);
int len;
unsigned char*
buffer;
// Get the length.
len = strlen(text);
// Save the text
in the buffer.
buffer = (unsigned
char*)text;
// Perform the
algorithm on each character
// in the string,
using the lookup table values.
while(len--)
ulCRC = (ulCRC >> 8) ^ crc32_table[(ulCRC & 0xFF) ^ *buffer++];
// Exclusive OR
the result with the beginning value.
return
ulCRC ^ 0xffffffff;
}
If you find this information useful, please make a donation
just a dollar would help
Create Window Home Page |
Programming
PC Magazine Utilities and source code written by -RAE-
ClipTrak Clipboard Organizer ClipTrak uses CRCs
DaysEase Calendar
This page is located online at:
32 bit Cyclic Redundancy Check Source Code for C++
Copyright © 2000 - 2008 Richard A. Ellingson
|