Shifts Operations, c++ cs and VB

otherside

Centurion
Joined
Mar 16, 2003
Messages
127
Location
UK - Greece
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++
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)
 
What is the error message?

Shouldn't &H8000, &H3FFFFFFF, &H1021 and &HFFFF be defined somewhere?

Shouldn't If (chx And &H8000) Then equal something?

I.E.
Visual Basic:
If strText = vbnullstring then

End if

Hope this helps.
 
I reconverted your code. Just a few notes about your VB: VB.Net does support the bitshift operator, and you are anding with 0x3FFFFFFF, which you weren't doing in the C++ code, which confused me a bit.

I couldn't test is and compare with the C++ code since I don't know what exactly the code does and I don't have a decent C++ compiler on this machine.
Visual Basic:
    Public Function calcrc(ByVal sfile As String) As Integer

        'int crc, i;
        Dim i, crc As Integer
        'char car;
        Dim car As Byte
        'int ola=0;
        Dim ola As Integer = 0

        'crc = 0;
        crc = 0

        'FILE *File;
        Dim FILE As IO.FileStream
        'File=fopen (file,"r");
        FILE = New IO.FileStream(sfile, IO.FileMode.Open)

        'fscanf (File,"%c",&car);
        car = CByte(FILE.ReadByte)
        'While ola! = EOF()
        Do Until FILE.Position = FILE.Length - 1
            '{
            'crc = crc xor (int)car << 8;
            crc = crc Xor CInt(car) << 8

            'for (i = 0; i < 8; ++i)
            For j As Integer = 0 To 7
                '{
                'if (crc & 0x8000)                   
                If CBool(crc & &H8000) Then
                    'crc = crc << 1 ^ 0x1021;
                    crc = crc << 1 Xor &H1021
                Else
                    'crc = crc << 1;
                    crc = crc << 1
                End If
            Next '}

            'ola=fscanf (File,"%c",&car);
            car = CByte(FILE.ReadByte)
        Loop '} 

        'fclose (File);
        FILE.Close()

        'return (crc & 0xFFFF);
        Return crc And &HFFFF
    End Function

SonicBoomAu said:
What is the error message?
I don't think there is an error message, I think that the code executes successfully but returns the wrong result.

SonicBoomAu said:
Shouldn't &H8000, &H3FFFFFFF, &H1021 and &HFFFF be defined somewhere?
They probably should have been defined as constants, but they weren't and this isn't a question about code practices.

SonicBoomAu said:
Shouldn't If (chx And &H8000) Then equal something?
Yes. And no. In VB, you generally want to use the equals operator because it returns a boolean value and VB requires a boolean value for "If" statements. In C++ (just like older versions of BASIC) any non-zero value is interpeted as true in an "If" statement. And in VB, with option strict off, any non-zero value is implicitly converted to a boolean True value, offering the same behavior.
 
Last edited:
I don't beleive all this time never tried to use the bit shift operation, i was under the impression that it was not included in vb.net

Anyway

and you are anding with 0x3FFFFFFF, which you weren't doing in the C++ code, which confused me a bit.

ok i understand your confusion, you have to thing in binary to get it :)

we are working with an integer which can have both possitive and negative values.
the max positive value is 0x7FFFFFFF.
the left shift operation is the same as multiplying by 2. the only difference is that in the shift operation the most significant bit when shifted is erased that way you don't have an overflow. But by multiplying with 2 you will get an overflow.
Now if the largest value is 0x7FFFFFFF this means that the last bit is not used in positive numbers. If you erase the least significant bit before the multiplication you won't get an overflow and will be the same as the shift operation. That is why i'm anding with 0x3FFFFFFF.

Your code does work but produces wrong result, i'll look in to it to get it solved.


Thanks again for the tips.
 
WELL YOU GUYS WILL NOT BELIEVE IT !!!

SORRY FOR WRITING IN CAPS, BUT SOMEDAY SOMEONE MIGHT NEED IT !!!

here is the deal,

my code was fine (as always hehehe) :PP

what is the MAIN difference between the programs ?

the way that the file is read.

on the c++ in order to read the file the person who wrote the code uses fscanf, which reads ASCII, of course if you consider it it's the same.
.......
except......

that fscanf IGNORES THE "CR" CHARACTER 0X0D.
it doesn't return it when it reads it from the file...

i think that the rest is obvious ....
 
otherside said:
ok i understand your confusion, you have to thing in binary to get it :)
I get it. You were multiplying instead of bitshifting, so you needed to take care of the most signifigant bit.

Well... glad you got the issue sorted out.
 
Back
Top