一段C++算法,请高手帮忙编辑成Pascal,谢谢! ( 积分: 50 )

  • 主题发起人 主题发起人 kingbenz
  • 开始时间 开始时间
K

kingbenz

Unregistered / Unconfirmed
GUEST, unregistred user!
#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 &amp
0x80)
crc_accum = (crc_accum << 1) ^ Local8BitPoly;
else
crc_accum = (crc_accum << 1);
}
crcTable = crc_accum
// Update next item in table
}


// calculate the CRC
for(j=0
j < SLIDE_ID_LEN
j++)
{
i = ((crc) ^ slideIdString[j]) &amp
0xff;
crc = (crc << 8) ^ crcTable;
}

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 &quot;140180001&quot;.
//
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, &quot;%011d&quot;, id);
printf(&quot;%s %03d/n&quot;, idstring, computeCrc(idstring));
}

return 0;
}
 
//#define SLIDE_ID_LEN 11 // Length of slide ID
const SLIDE_IN_LEN = 11;
// This function computes and returns the CRC associated with the slideIDString input
// argument.

function computerCrc( var slideString:string):byte;
var
Local8BitPoly : byte=$025;
crcTable : array[0..255] of byte;
i, j : integer;
crc : byte;
crc_accum : byte;
begin
for i := 0 to 255 do
begin
crc_accum := byte(i);

for j :=0 to 7 do
begin
if(crc_accum ==$80) then
crc_accum := (crc_accum shl 1) xor Local8BitPoly
else
crc_accum := (crc_accum shl 1);
end;
crcTable := crc_accum;// Update next item in table

// calculate the CRC
for j := 0 to SLINDE_ID_LEN-1 do
begin
i := (( crc shl 8 ) xor slideIdString[j]) and $FF;
crc := (crc shl 8 ) xor crcTable;
end;
result := crc;
end;
 
//完整代码--
program crc;

{$APPTYPE CONSOLE}

uses
SysUtils;
// This function computes and returns the CRC associated with the slideIDString input
// argument.
//
const SLIDE_ID_LEN = 11;
function computeCrc( var slideIdString:string):byte;
var
Local8BitPoly : byte;
crcTable : array[0..255] of byte;
i, j : integer;
crc : byte;
crc_accum : byte;
begin
crc := 0;
Local8BitPoly := $025;
for i := 0 to 255 do
begin
crc_accum := byte(i);

for j :=0 to 7 do
begin
if(crc_accum =$80) then
crc_accum := (crc_accum shl 1) xor Local8BitPoly
else
crc_accum := (crc_accum shl 1);
end;
crcTable := crc_accum;// Update next item in table
end;
// calculate the CRC
for j := 1 to SLIDE_ID_LEN do
begin
i := (( crc shl 8 ) xor strtoint(slideIdString[j])) and $FF;
crc := (crc shl 8 ) xor crcTable;
end;

result := crc;
end;
// 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 &quot;140180001&quot;.
//

{
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, &quot;%011d&quot;, id);
printf(&quot;%s %03d/n&quot;, idstring, computeCrc(idstring));
}
var
id : longword= 14080001;
idString : string;
i : integer;
begin
{ TODO -oUser -cConsole Main : Insert code here }
for I := 0 to 24 do //
begin
idString := format('%11.11d',[id]);
writeln(idstring,',crc=', format('%3.3d',[computeCrc(idstring)]));
id := id +1;
end;
readln;
end.
 
program crc;

{$APPTYPE CONSOLE}

uses
SysUtils;
// This function computes and returns the CRC associated with the slideIDString input
// argument.
//
const SLIDE_ID_LEN = 11;
function computeCrc( var slideIdString:string):byte;
var
Local8BitPoly : byte;
crcTable : array[0..255] of byte;
i, j : integer;
crc : byte;
crc_accum : byte;
begin
crc := 0;
Local8BitPoly := $025;
for i := 0 to 255 do
begin
crc_accum := byte(i);
for j :=0 to 7 do
begin
if(crc_accum =$80) then
crc_accum := (crc_accum shl 1) xor Local8BitPoly
else
crc_accum := (crc_accum shl 1);
end;
crcTable := crc_accum;// Update next item in table
end;
// calculate the CRC
for j := 1 to SLIDE_ID_LEN do
begin
i := (( crc shl 8 ) xor strtoint(slideIdString[j])) and $FF;
crc := (crc shl 8 ) xor crcTable;
end;
result := crc;
end;
// 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 &quot;140180001&quot;.
var
id : longword= 14080001;
idString : string;
i : integer;
begin
{ TODO -oUser -cConsole Main : Insert code here }
for I := 0 to 24 do //
begin
idString := format('%11.11d',[id]);
writeln(idstring,',crc=', format('%3.3d',[computeCrc(idstring)]));
id := id +1;
end;
readln;
end.
 
感谢enterwin,但是出来的校验码不正确,用C编辑出来正确的校验码是:
00140180001 206
00140180002 161
00140180003 132
00140180004 127
00140180005 090
00140180006 053
00140180007 016
00140180008 230
00140180009 195
00140180010 110
00140180011 075
00140180012 036
00140180013 001
00140180014 250
00140180015 223
00140180016 176
00140180017 149
00140180018 099
00140180019 070
00140180020 196
00140180021 225
00140180022 142
00140180023 171
00140180024 080
00140180025 117
 
在CSDN得到正确算法了
{$APPTYPE CONSOLE}

uses
SysUtils;

const
SLIDE_ID_LEN = 11
// Length of slide ID

function 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 := 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);
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.
 
//不好意思,有点粗心了
program crc;

{$APPTYPE CONSOLE}

uses
SysUtils;
// This function computes and returns the CRC associated with the slideIDString input
// argument.
//
const SLIDE_ID_LEN = 11;
function computeCrc( var slideIdString:string):byte;
var
Local8BitPoly : byte;
crcTable : array[0..255] of byte;
i, j : integer;
crc : byte;
crc_accum : byte;
begin
crc := 0;
Local8BitPoly := $025;
for i := 0 to 255 do
begin
crc_accum := byte(i);
for j :=0 to 7 do
begin
if(crc_accum and $80 <> 0) then
crc_accum := (crc_accum shl 1) xor Local8BitPoly
else
crc_accum := (crc_accum shl 1);
end;
crcTable := crc_accum;// Update next item in table
end;
// calculate the CRC
for j := 1 to SLIDE_ID_LEN do
begin
i := ( crc xor byte(slideIdString[j])) and $FF;
crc := byte((crc shl 8 ) xor crcTable);
end;
result := crc;
end;
// 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 &quot;140180001&quot;.
var
id : longword= 140180001;
idString : string;
i : integer;
begin
{ TODO -oUser -cConsole Main : Insert code here }
for I := 0 to 24 do //
begin
idString := format('%.11d',[id]);
writeln(idstring,',crc=', format('%3.3d',[computeCrc(idstring)]));
id := id +1;
end;
readln;
end.
 

Similar threads

后退
顶部