Hi. I'am trying calculate CS1 but get wrong CS each time. Maybe someone will tell me what I'm doing wrong.
Example block (from previous posts) :
36 00 F2 78 E2 09 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 6F 79 00 00 B1 6F 00 00 85 7C 88 7C
Original C code:
unsigned long crc_buffer(unsigned long crc, const unsigned long *p, unsigned long sz)
{
const unsigned long poly = 0xEDB88320;
unsigned long tmp1, tmp2;
while(sz--)
{
tmp1 = 0;
tmp2 = crc & poly;
for(int i = 0; i <=31; i++ )
tmp1 ^= ((tmp2 >> i) & 1);
crc = *p++ ^ ((crc << 1) | tmp1) ;
}
return crc;
}
Pascal code from
Auto-elect (post 70). He claiming that function works. As I can see this Pascal code is identical to C code except for one difference: in While loop i C we counting from last element to first, in pascal - from first to last.
function CRC32T(Initial: LongWord; Data:array of byte; DataSize: LongWord): LongWord;
const poly=$EDB88320;
var j:integer;
tmp1,tmp2,crc,i:longword;
BEGIN
crc:=initial;
i:=0;
while i<=DataSize-1 do begin
tmp1:=0;
tmp2:=crc and poly;
for j:=0 to 31 do tmp1:=tmp1 xor ((tmp2 shr j) and 1);
crc:=Data[i] xor ((crc shl 1) or tmp1);
i:=i+1;
end;
CRC32T:=crc;
END;
Question regarding input parameters: 1) Initial - understood; 2) Data -> data to checksum (byte array) - understood; 3) DataSize - ? Size of what? of byte array? In this case we don't need this parameter, we can calculate it inside function.
My code in Visual Basic:
Public Function GetCRC32_eeprom(Data As Byte(), CRC_initial As UInteger) As String
Dim crc As UInteger = CRC_initial
Dim poly As UInteger = &HEDB88320UI
Dim tmp1 As UInteger
Dim tmp2 As UInteger
Dim size As Integer = Data.Length - 1
While size >= 0
tmp1 = 0
tmp2 = crc And poly
For i = 0 To 31
tmp1 = tmp1 Xor ((tmp2 >> i) And 1)
Next
crc = Data(size) Xor ((crc << 1) Or tmp1)
size = size - 1
End While
Return Hex(crc)
End Function
With example block i can't get
xx xx F2 78 checksum. Tried swap data (2bytes and 4 bytes).
Please give me hint what I am doing wrong.