Autor Beitrag
Didi1952
Hält's aus hier
Beiträge: 1

xp
Delphi
BeitragVerfasst: Fr 04.03.05 15:38 
Hallo zusammen,
habe folgendes Problem:

Muß eine CRC Prüfsumme bilden, Prog ist aber in C_code:

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
/*************************************************************************
/ unsigned int crc_calc( unsigned char *data_buffer, 
                         unsigned char data_length) 
/*************************************************************************
/ 
{
  //local declarations 
  unsigned int temp, crc16; 
  unsigned char loop, j; 

  // Code starts here 
  crc16 = 0xFFFF;     //Init-value 

  for ( loop = 0; loop < data_length; loop ++) 
  { 
    crc16 ^= *data_buffer ++;         //crc16 XOR data_buffer[ loop] 

    for (j = 0; j < 8; j++)           //do 8 times 
    { 
      temp = crc16; 
      crc16 >>= 1;                    // shift right 
      if (temp & 0x0001)              // LSB = 1 ? 
       crc16 ^= 0xA001;               // -> crc16 XOR 0xA001 
    } 
  } 
  return (crc16);                    // Return CRC-Code (16 Bit) 
} 
/*************************************************************************/


Kann mir jemand helfen das Ding in Delphi umzuschreiben. Ich bekomme das einfach nicht auf die Reihe.


D.Gröger

Moderiert von user profile iconTino: Code-Tags hinzugefügt.
tommie-lie
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 4373

Ubuntu 7.10 "Gutsy Gibbon"

BeitragVerfasst: Fr 04.03.05 16:29 
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
/*************************************************************************
/ unsigned int crc_calc( unsigned char *data_buffer, 
                         unsigned char data_length) 
/*************************************************************************
/ 
{
  //local declarations 
  unsigned int temp, crc16; 
  unsigned char loop, j; 

  // Code starts here 
  crc16 = 0xFFFF;     //Init-value 

  for ( loop = 0; loop < data_length; loop ++) 
  { 
    crc16 ^= *data_buffer ++;         //crc16 XOR data_buffer[ loop] 

    for (j = 0; j < 8; j++)           //do 8 times 
    { 
      temp = crc16; 
      crc16 >>= 1;                    // shift right 
      if (temp & 0x0001)              // LSB = 1 ? 
       crc16 ^= 0xA001;               // -> crc16 XOR 0xA001 
    } 
  } 
  return (crc16);                    // Return CRC-Code (16 Bit) 
} 
/*************************************************************************/

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
function crc_calc(data_buffer: Pointer; data_length: Byte): Word;
var
  temp, crc16: Word;
  loop, j: Integer;
begin
  crc16 := $FFFF; // init value
  for loop := 0 to data_length - 1 do
  begin
    crc16 := crc16 xor PByte(LongWord(data_buffer) + loop)^; // CRC xor data_buffer[loop]

    for j := 0 to 7 do // do 8 times
    begin
      temp := crc16;
      crc16 := crc16 shr 1; // shift right
      if (temp and $0001) = 1 then // LSB = 1 ?
        crc16 := crc16 xor $A001; // Return CRC-Code (16 Bit)
    end;
  end;

  Result := crc16;
end;


Edit: bad style type cast eliminiert ;-)

_________________
Your computer is designed to become slower and more unreliable over time, so you have to upgrade. But if you'd like some false hope, I can tell you how to defragment your disk. - Dilbert