Crc.gif (968 bytes)

Cyclic Redundancy Code Calculator 

Lab Report

Calculate CRC of Character String using CRC Calculator
(Also see the FileCheck Lab Report for CRCs of Files, Directories, Volumes)
Delphi 7 CLX Version (Windows)
ScreenCRCCalc.jpg (17211 bytes)

Calculate CRC of File using CRC Calculator
Kylix 3 CLX Version (Linux)

The purpose of the CRCCalculator is to display interactively the CRC-16 and CRC-32 values for a specified string or file.  (The CRC-32 values will match those computed by PKZIP.)

Additional files, CRC16Dem and CRC32Dem, show how to create a command-line program to calculate CRC values.   A "C" command-line program is also available.

Materials and Equipment

Software Requirements
Windows 95/98/2000 and Delphi 3/4/5/6/7 (to recompile)
Linux and Kylix 3 (to recompile)

Hardware Requirements
VGA display


  1. Double click on the CRCCalculator.EXE icon to start the program (Windows), or run the program under Linux using ./CrcCalculator..
  2. Enter any text in the edit box.
  3. Observe the CRC-16 and CRC-32 values in decimal or hexadecimal.
  4. Select File tabsheet.
  5. Click on the Read button and select a file
  6. Observe the CRC-16 and CRC-32 values in decimal or hexadecimal.

Expected Values
Hex values do not change by version of Delphi, but the decimal values are intended to be unsigned.  CRC-16 values have always been unsigned, but since there was no 4-byte unsigned integer in D1-D3, the decimal values are signed for the CRC-32 until the Delphi 4 version.


Test String

Init Method 1
Init Method 2
Init Method 2


Decimal Hex Decimal Hex Decimal Hex
<null string> 0 0000 0000  0 00000000 Delphi 2 - 7, Kylix 3
abc 38712 9738 43190 A8B6  891568578 352441C2 Delphi 2 - 7, Kylix 3
ABC 38712 4521 31407 7AAF  -1551695032 A3830348 Delphi 2 - 3
2743272264 Delphi 4 - 7, Kylix 3
This is a string 19524 4C44 17157 4305 141976383 0876633F Delphi 2 - 7, Kylix 3

See the Initialization/Finalization discussion below for a description of Initialization Methods 1 and 2.

See Felipe Rocha Machado's comments about printing 32-bit integers in D3.

The CRC-32s of the files abcLower.TXT, ABCupper.TXT, and ThisIsAString.TXT in the CRCDelphi.ZIP file match the values above, which are also verified in the CRC32Dem.PAS command line program:

CRC-32    Bytes         F i l e n a m e
-------- -------- ------------------------
352441C2        3 abcLower.TXT
A3830348        3 ABCUpper.TXT
0876633F       16 ThisIsAString.TXT

CRC values, especially the CRC-32, are an extremely good way to verify the integrity of a file.  If the CRC-32 for a file stays the same, there is only an extremely small probability that the file has been changed -- about 1 in 4 billion.   CRCs could be used as a preliminary verification tool to find identical files.   If the CRCs of two files do not match, the file are not the same.  This could even be used to compare image files.

Lookup Tables.  The "hardware" method of computing CRCs involves bit manipulations, which is very inefficient for a software computation.  Instead of computing the CRC bit-by-bit, a 256-element lookup table can be used to perform the equivalent of 8 bit operations at a time.  (This is described in "Byte-wise CRC Calculations" in IEEE Micro, June 1983, pp. 40-50.) 

For a CRC-16, the lookup table consists of 256 2-byte WORDs (see below, or the CRC16.PAS unit for the actual table, or the CRCTable program for computation of the lookup table for the x16 + x15 + x2 + 1 generator polynomial):

  table: ARRAY[0..255] OF WORD =

Given the above  lookup table, the code for computing a CRC-16 is as follows (see initialization/finalization below):

PROCEDURE CalcCRC16 (p: pByte; nbyte: WORD; VAR CRCvalue: WORD);
    i: WORD;
    q: pByte;

  {The following is a little cryptic (but executes very quickly).
   The algorithm is as follows:
   1. exclusive-or the input byte with the low-order byte of
      the CRC register to get an INDEX
   2. shift the CRC register eight bits to the right
   3. exclusive-or the CRC register with the contents of
   4. repeat steps 1 through 3 for all bytes}
  q := p;
  FOR i := 1 TO nBYTE DO
    CRCvalue := Hi(CRCvalue) XOR Table[ q^ XOR Lo(CRCvalue) ];
END {CalcCRC16};

For a CRC-32, the lookup table consists of 256 4-byte DWORDs (also see the CRC32.PAS unit).  

// The constants here are for the CRC-32 generator 
// polynomial, as defined in the Microsoft 
// Systems Journal, March 1995, pp. 107-108

  table: ARRAY[0..255] OF DWORD =
 ($00000000, $77073096, $EE0E612C, $990951BA,
  $076DC419, $706AF48F, $E963A535, $9E6495A3,
  $0EDB8832, $79DCB8A4, $E0D5E91E, $97D2D988,
  $09B64C2B, $7EB17CBD, $E7B82D07, $90BF1D91,
  $1DB71064, $6AB020F2, $F3B97148, $84BE41DE,
  $1ADAD47D, $6DDDE4EB, $F4D4B551, $83D385C7,
  $136C9856, $646BA8C0, $FD62F97A, $8A65C9EC,
  $14015C4F, $63066CD9, $FA0F3D63, $8D080DF5,
  $3B6E20C8, $4C69105E, $D56041E4, $A2677172,
  $3C03E4D1, $4B04D447, $D20D85FD, $A50AB56B,
  $35B5A8FA, $42B2986C, $DBBBC9D6, $ACBCF940,
  $32D86CE3, $45DF5C75, $DCD60DCF, $ABD13D59,
  $26D930AC, $51DE003A, $C8D75180, $BFD06116,
  $21B4F4B5, $56B3C423, $CFBA9599, $B8BDA50F,
  $2802B89E, $5F058808, $C60CD9B2, $B10BE924,
  $2F6F7C87, $58684C11, $C1611DAB, $B6662D3D,

  $76DC4190, $01DB7106, $98D220BC, $EFD5102A,
  $71B18589, $06B6B51F, $9FBFE4A5, $E8B8D433,
  $7807C9A2, $0F00F934, $9609A88E, $E10E9818,
  $7F6A0DBB, $086D3D2D, $91646C97, $E6635C01,
  $6B6B51F4, $1C6C6162, $856530D8, $F262004E,
  $6C0695ED, $1B01A57B, $8208F4C1, $F50FC457,
  $65B0D9C6, $12B7E950, $8BBEB8EA, $FCB9887C,
  $62DD1DDF, $15DA2D49, $8CD37CF3, $FBD44C65,
  $4DB26158, $3AB551CE, $A3BC0074, $D4BB30E2,
  $4ADFA541, $3DD895D7, $A4D1C46D, $D3D6F4FB,
  $4369E96A, $346ED9FC, $AD678846, $DA60B8D0,
  $44042D73, $33031DE5, $AA0A4C5F, $DD0D7CC9,
  $5005713C, $270241AA, $BE0B1010, $C90C2086,
  $5768B525, $206F85B3, $B966D409, $CE61E49F,
  $5EDEF90E, $29D9C998, $B0D09822, $C7D7A8B4,
  $59B33D17, $2EB40D81, $B7BD5C3B, $C0BA6CAD,

  $EDB88320, $9ABFB3B6, $03B6E20C, $74B1D29A,
  $EAD54739, $9DD277AF, $04DB2615, $73DC1683,
  $E3630B12, $94643B84, $0D6D6A3E, $7A6A5AA8,
  $E40ECF0B, $9309FF9D, $0A00AE27, $7D079EB1,
  $F00F9344, $8708A3D2, $1E01F268, $6906C2FE,
  $F762575D, $806567CB, $196C3671, $6E6B06E7,
  $FED41B76, $89D32BE0, $10DA7A5A, $67DD4ACC,
  $F9B9DF6F, $8EBEEFF9, $17B7BE43, $60B08ED5,
  $D6D6A3E8, $A1D1937E, $38D8C2C4, $4FDFF252,
  $D1BB67F1, $A6BC5767, $3FB506DD, $48B2364B,
  $D80D2BDA, $AF0A1B4C, $36034AF6, $41047A60,
  $DF60EFC3, $A867DF55, $316E8EEF, $4669BE79,
  $CB61B38C, $BC66831A, $256FD2A0, $5268E236,
  $CC0C7795, $BB0B4703, $220216B9, $5505262F,
  $C5BA3BBE, $B2BD0B28, $2BB45A92, $5CB36A04,
  $C2D7FFA7, $B5D0CF31, $2CD99E8B, $5BDEAE1D,

  $9B64C2B0, $EC63F226, $756AA39C, $026D930A,
  $9C0906A9, $EB0E363F, $72076785, $05005713,
  $95BF4A82, $E2B87A14, $7BB12BAE, $0CB61B38,
  $92D28E9B, $E5D5BE0D, $7CDCEFB7, $0BDBDF21,
  $86D3D2D4, $F1D4E242, $68DDB3F8, $1FDA836E,
  $81BE16CD, $F6B9265B, $6FB077E1, $18B74777,
  $88085AE6, $FF0F6A70, $66063BCA, $11010B5C,
  $8F659EFF, $F862AE69, $616BFFD3, $166CCF45,
  $A00AE278, $D70DD2EE, $4E048354, $3903B3C2,
  $A7672661, $D06016F7, $4969474D, $3E6E77DB,
  $AED16A4A, $D9D65ADC, $40DF0B66, $37D83BF0,
  $A9BCAE53, $DEBB9EC5, $47B2CF7F, $30B5FFE9,
  $BDBDF21C, $CABAC28A, $53B39330, $24B4A3A6,
  $BAD03605, $CDD70693, $54DE5729, $23D967BF,
  $B3667A2E, $C4614AB8, $5D681B02, $2A6F2B94,
  $B40BBE37, $C30C8EA1, $5A05DF1B, $2D02EF8D);

Given the above  lookup table, the code for computing a CRC-32 is as follows (see initialization/finalization below):

// Use CalcCRC32 as a procedure so CRCValue can be passed in but
// also returned. This allows multiple calls to CalcCRC32 for
// the "same" CRC-32 calculation.
PROCEDURE CalcCRC32 (p: pointer; ByteCount: DWORD; VAR CRCValue: DWORD);
  // The following is a little cryptic (but executes very quickly).
  // The algorithm is as follows:
  // 1. exclusive-or the input byte with the low-order byte of
  // the CRC register to get an INDEX
  // 2. shift the CRC register eight bits to the right
  // 3. exclusive-or the CRC register with the contents of Table[INDEX]
  // 4. repeat steps 1 through 3 for all bytes

    i:  DWORD;
    q: ^BYTE;
  q := p;
  FOR i := 0 TO ByteCount-1 DO
    CRCvalue := (CRCvalue SHR 8) XOR
    Table[ q^ XOR (CRCvalue AND $000000FF) ];
END {CalcCRC32};

You can pass nearly any argument to this routine since the first parameter is a pointer.  For a string, pass the address of the first character, for example:

     CalcCRC32 (Addr(s[1]), LENGTH(s), CRC32);

To avoid an access violation in Delphi 4 (or later) make sure Length(s) > 0.  (I'm not sure why Delphi 3 didn't complain.)

This routine can be used to verify the CRC32 table of constants has not been accidentally modified.  The following code in the CRC32 unit initialization checks the 1024-byte array of DWORDs:

  CRC32Table: DWORD;

  // Verify the table used to compute the CRCs has not been modified.
  // Thanks to Gary Williams for this suggestion, Jan. 2003.
  CRC32Table := $FFFFFFFF;
  CalcCRC32 (Addr(table[0]), SizeOf(table), CRC32Table);
  CRC32Table := NOT CRC32Table;

  IF CRC32Table <> $6FCF9E13
  THEN ShowMessage('CRC32 Table CRC32 is ' + 
                   IntToHex(Crc32Table, 8) +
                   ', expecting $6FCF9E13');
END {CRC32}.

To compute the same CRC-32 as used in the PKZIP utility, start with a CRCvalue of $FFFFFFFF.  After calling CalcCRC32 above (any number of times), the finalization consists of a 1's complement of the CRCvalue.  This can be computed with the expression NOT CRCvalue in Delphi.  See additional details in the next section.

Initialization and Finalization.  The initialization and finalization of the CRC computation are arbitrary.   Many years ago when I first started computing CRCs, I set the initial value to 0 and did no finalization -- this is "Method 1" described above under Expected Values

CRC16 := 0;
IF LENGTH(s) > 0 // Avoid access violation in D4
THEN CalcCRC16 (Addr(s[1]), LENGTH(s), CRC16);

The "standard" CRC-32 (the one used by PKZIP) starts with $FFFFFFFF as the initial value and then performs a 1's complement to yield the final value -- this is "Method 2" described above under Expected Values.  Here's what is done in the CRC Calculator for CRC-32s:

CRC32 := $FFFFFFFF; // To match PKZIP
IF   LENGTH(s) > 0  // Avoid access violation in D4
THEN CalcCRC32 (Addr(s[1]), LENGTH(s), CRC32);
CRC32 := NOT CRC32; // TO match PKZIP

In the CRC16 computation the initial value is $FFFF with Method 2.

[Thanks to Rolf Gebhardt and Glen Harman for pointing out an inconsistency about how finalization was handled in an earlier version of this article.]

CRC of a File.  All the bytes of a file must be passed to the CalcCRC routines, i.e., CalcCRC16 and CalcCRC32, to compute the CRC of a file.  The older BlockRead I/O primitive is used in the CalcFileCRC16 routine in the CRC16 unit since BlockRead at one point was the only way to read a binary stream of bytes.  CalcFileCRC32 uses the more contemporary memory stream to read the bytes of a file (when the StreamIO conditional compilation is defined).

// Use MemoryStream to read file in binary mode.
    VAR TotalBytes: TInteger8;
    VAR error: WORD);
    Stream: TMemoryStream;
  error := 0;
  CRCValue := $FFFFFFFF;
  Stream := TMemoryStream.Create;
      IF   Stream.Size > 0
      THEN CalcCRC32 (Stream.Memory, Stream.Size, CRCvalue)
      ON E: EReadError DO
      error := 1

    CRCvalue := NOT CRCvalue;
    TotalBytes := Stream.Size
END {CalcFileCRC32};

The above procedure assumes that the file will easily fit into memory in a TMemoryStream.  Unfortunately, this can be a bad assumption especially when some files are bigger than physical memory.  For example, processing a 1 GB file in a memory stream with only 512 MB of physical memory might at minimum will tax the virtual memory processing of the operating system.  For now, this isn't much of a problem.

See Ted Tøraasen's modification for CRC-16s for buffers > 64 KB.

FileCheck Program.  See the FileCheck Lab Report for information about creating CRCs of files, directories, or even whole volumes.  ("Meta" CRCs -- that is, CRCs of CRCs of a well-ordered list of files -- used to detect changes in directories or whole disk volumes.)

Command Line Programs.  The command line examples, CRC16Dem and CRC32Dem can be compiled from a DOS Window (assuming your path contains the Delphi bin directory) by entering:

     DCC32 CRC16Dem.PAS    or
     DCC32 CRC32Dem.PAS

Study the CRC16Dem and CRC32Dem command line programs for a way to calculate CRCs without a Windows interface.

The Delphi installation CD has a file CRC32.C, which shows how to compute CRC-32s, as well as the lookup table, in the directory \Info\Extras\Zlib\Src.

CRC Links

A Painless Guide to CRC Error Detection Algorithms 

Peter Haas' Delphi unit for demonstration calculation of CRC, based on the document: "A Painless Guide to CRC Error Detection Algorithms"

Peter Haas' unit contains functions to calculate a arbitrary CRC (up to 32 bit) by given parameters (Polynom, Init, XorOut, ReflectIn, ReflectOut). A
another part is the generation of a Lookup Table and the calculation with this table. The unit can also used to find the parameters for a unknown CRC calculation with trial and error. Last but not least, it contain the unit a function, that creates Delphi source to calculate a CRC with the given parameters in a separate application.

Find code for CRC-16 CCITT here:

Cyclic Redundancy Check Computation (Texas Instruments Application Report)
- Coding theory behind CRC
- Algorithms for CRC computation 

"For the Love of the Game" by Michael Barr, Embedded Systems Programming, Dec. 1999, pp. 47-54. 

"Slow and Steady Never Lost the Race" by Michael Barr, Embedded Systems Programming, Jan. 2000, pp. 37-46.  Shows how to compute CRC lookup table. 

The CRC Pitstop is a repository for information on CRC and other checking algorithms 

CRC and How to Reverse it  (in Russian)

CRC - Der Cyclic Redundancy Code (in German)

Understanding Cyclic Redundancy Check 

Robert Lee's optimized code for CRC computation: 

Checksum-Algorithms: XOR16, XOR32, CRC32

Steve Schafer's UseNet Post showing calculation of CRC-32 Lookup Table

Björn Kriedemann's UseNet Post with CRC Unit from April 1997 DDJ:  CRC16, XYZModemCRC16, CRC32

Lars Truijens's UseNet Post showing Delphi code for XModem CRC-16 (X16 + X12 + X5 + 1) with a Lookup Table

CRC-16 (X16+X15+X2+1) without lookup table, .

CRC-16 Calculator in Visual Basic by Stuart Rolfe

A CRC Calculator Unit provides three speed-optimized functions to compute (or continue computation of) a Cyclic Redundancy Check (CRC). Applicable to XModem protocol (16-bit CRC), SEA's "ARC" utility, PKZip (32-bit CRC) and many others compatible software, .

SWAG (Software Archive Group) CRC Snipets:  Includes various CRC and Checksum routines (requires "Reader" program to view)

Algorithm of CRC-32 calculation for file 

"Calculating CRC Checksums in C++" by Colin Mahoney in June 1999 C/C++ Users Journal.

Algorithms Alfresco: Whirlpool (CRC Algorithms), Julian Bucknall unravels CRC, Delphi Magazine, Issue 48, August 1999.

Split and Join  (use CRCs to verify copy is correct after a file is split into separate floppy-size files and later rejoined) 

CRC-32 used in PNG graphics file format 

The D7 "Companion Tools" (Disk 1) has a directory \nag_software_solutions\crc32_library with a CRC32.EXE file from NAG Software Solutions.

Implementing CRCCs in Altera Devices 

See the CRC32 Library on CD #1 of the Delphi Studio Companion Tools.


Check Digits (credit cards and the "Modulo 10"  check digit algorithm) 

MD5 Homepage (unofficial)

Useful literature:
"Procedure for Computing CRC-32 Values," Microsoft Systems Journal, March 1995, pp. 107-108.

"Byte-wise CRC Calculations" by Aram Perez in IEEE Micro, June 1983, pp. 40-50. Shows how to create a lookup table which is the best way to implement in software (versus the shifts that are done when implemented in hardware). 

"A Tutorial on CRC Computations" by Tenkasi V. Ramabadran and Sunil S. Gaitonde in IEEE Micro, August 1988, pp. 62-75.

"Cyclic Redundancy Checks for Data Integrity or Identity" by William H. Press and Saul A. Teukolsky, Computers in Physics, Jul/Aug 1989, pp. 88-91.

Standard Name SDLC (CCITT) (X25) CRC-16  CRC-32 (Ethernet)
Width 16 bits 16 bits 32 bits
Generator Polynomial 10001000000100001

x16 + x12 + x5 + 1


x16 + x15 + x2 + 1


x32 + x26 + x23 + x22 + x16 + x12 + x11
x10 + x8 + x7 + x5 + x4 + x2 + x1 + 1

Initial remainder 0xFFFF 0x0000 0xFFFFFFFF
Final XOR value 0x0000 0x0000 0xFFFFFFFF

Other standard polynomials:
CRC-16 Reverse:  x16 + x14 + x1 + 1
SDLC Reverse:  x16 + x11 + x4 + 1
CRC-12:  x12 + x11 + x3 + x2 + x1 + 1

CRC values, especially the CRC-32, are an extremely good way to verify the integrity of a string or even a  file.

cyclic redundancy check, CRC-16, CRC-32, APPTYPE CONSOLE, lookup table, XOR, Comp, Int64, IntToHex, Addr, Delphi, Kylix


CLX (Component Library for Cross-Platform -- Windows or Linux)

VCL (Visual Component Library -- Windows only)

Borland C++ 5.02 "C" CRC-32 Source and EXE (45 KB):  CRCc.ZIP
Use the "make file" to compile:  make -f crc32.mak
Modify .mak file to point to correct location of wildargs.obj.  The .mak file automatically performs a test that the results will match those of PKZIP.)  This command-line utility can be used with wildcards to find the CRC-32 of files in a directory, for example:  crc32 *.*

TuboPascal 5.5 program to compute values for CRC-16 lookup table.

Updated 14 Jun 2009
Since 29 Nov 1998