otherside
Centurion
Ok guys i'm losing it here... so any expert who can spare a few minutes take a look.
i'm trying to calcute a crc type thing. The code work fine on c++, but doesn't procude the correct result in c# or vb and i really don't get why.
Here is the code in C++
The code in c#
and the code in VB.NET
Ok guys if anyone has any idea why the first one produces the correct the the other not please help.
(Note the c# and the VB produces the same result)
i'm trying to calcute a crc type thing. The code work fine on c++, but doesn't procude the correct result in c# or vb and i really don't get why.
Here is the code in C++
Code:
int calcrc(char *file)
{
int crc, i;
char car;
int ola=0;
crc = 0;
FILE *File;
File=fopen (file,"r");
fscanf (File,"%c",&car);
while (ola!=EOF )
{
crc = crc ^ (int)car << 8;
for (i = 0; i < 8; ++i)
{
if (crc & 0x8000)
crc = crc << 1 ^ 0x1021;
else
crc = crc << 1;
}
ola=fscanf (File,"%c",&car);
}
fclose (File);
return (crc & 0xFFFF);
}
The code in c#
Code:
public int CRC(byte[] bf,int Len)
{
int crc =0, i,j;
for (j=0;j<Len;j++)
{
crc = crc ^ (int)bf[j] <<8;
for (i = 0; i < 8; ++i)
{
if ((crc & 0x8000) !=0)
{
crc = crc << 1 ^ 0x1021;
}
else
{
crc = crc << 1;
}
}
}
return crc;
}
and the code in VB.NET
Code:
Dim fl As New System.IO.FileStream(OpenFileDialog1.FileName, IO.FileMode.Open)
Dim chx As Integer
Dim i, j As Integer
For i = 0 To fl.Length - 1
chx = chx Xor (CType(fl.ReadByte(), Integer) * 256)
For j = 0 To 7
If (chx And &H8000) Then
chx = chx And &H3FFFFFFF
chx = (chx * 2) Xor &H1021
Else
chx = chx And &H3FFFFFFF
chx = chx * 2
End If
Next
Next
fl.Close()
MessageBox.Show(chx And &HFFFF)
Ok guys if anyone has any idea why the first one produces the correct the the other not please help.
(Note the c# and the VB produces the same result)