#include <stdio.h>#define SLIDE_ID_LEN 11 // Length of slide ID// This function computes and returns the CRC associated with the slideIDString input 
// argument.
//  
unsigned char computeCrc(char *slideIdString)
{
unsigned char Local8BitPoly = 0x025;
unsigned char crcTable[256];
int i,j;
unsigned char crc = 0;

// Generate CRC table
unsigned char crc_accum;
for(i=0; i<256; i++)
{
crc_accum = (unsigned char) i;

for(j=0; j<8; j++)
{ // If lead 1 then XOR to divide else move on.
if(crc_accum & 0x80)
crc_accum = (crc_accum << 1) ^ Local8BitPoly;
else
crc_accum = (crc_accum << 1);
}
crcTable[i] = crc_accum;  // Update next item in table
}
// calculate the CRC
for(j=0; j < SLIDE_ID_LEN; j++)
{
  i = ((crc) ^ slideIdString[j]) & 0xff;
  crc = (crc << 8) ^ crcTable[i];
} return crc;
}
// This main program provides an example of how to use the computeCrc function.
// In this example, the CRC's are computed and displayed for a range of 25 ID's starting 
// with the ID value of "140180001".
//
int main(int argc, char* argv[])
{
long id = 140180001;
char idstring[SLIDE_ID_LEN+1];
int i; // Generate CRC for 25 values
for (i=0; i < 25; i++, id++)
{
sprintf(idstring, "%011d", id);
printf("%s   %03d\n", idstring, computeCrc(idstring));
} return 0;
}

解决方案 »

  1.   

    program Project2;{$APPTYPE CONSOLE}uses
      SysUtils;const
      SLIDE_ID_LEN = 11; // Length of slide IDfunction computeCrc(slideIdString: PChar): Byte;
    var
      Local8BitPoly: Byte;
      crcTable: array[0..255] of Byte;
      i, j: Integer;
      crc_accum: Byte;
    begin
      Result := 0;
      Local8BitPoly := $025;
      // Generate CRC table
      for i := 0 to 255 do
      begin
        crc_accum := Byte(i);
        for j := 0 to 7 do
        begin
          // If lead 1 then XOR to divide else move on.
          if crc_accum and $80 <> 0 then
            crc_accum := Byte((crc_accum shl 1) xor Local8BitPoly)
          else crc_accum := (crc_accum shl 1);
        end;
        crcTable[i] := crc_accum;  // Update next item in table
      end;  // calculate the CRC
      for j := 0 to SLIDE_ID_LEN - 1 do
      begin
        i := (Result xor Byte(slideIdString[j])) and $ff;
        Result := Byte((Result shl 8) xor crcTable[i]);
      end;
    end;var
      id: Longint = 140180001;
      idstring: array [0..SLIDE_ID_LEN - 1] of Char;
      i: Integer;begin
      // Generate CRC for 25 values
      for i := 0 to 24 do
      begin
        SysUtils.FormatBuf(idstring[0], SizeOf(idstring), '%.11d', 5, [id]);
        Writeln(Format('%s   %.3d', [idstring, computeCrc(idstring)]));
        Inc(id);
      end;
      Readln;
    end.
      

  2.   

    如果是找 CRC 的算法,可以看一下 CRC Calculatorhttp://www.utilmind.com/delphi1.html
      

  3.   

    直接盒子下个CRC算法用就是了