otherside Posted November 2, 2005 Posted November 2, 2005 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++ 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# 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 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) Quote
SonicBoomAu Posted November 2, 2005 Posted November 2, 2005 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. If strText = vbnullstring then End if Hope this helps. Quote Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -- Rick Cook, The Wizardry Compiled
Leaders snarfblam Posted November 2, 2005 Leaders Posted November 2, 2005 (edited) 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. 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 What is the error message?[/Quote] I don't think there is an error message, I think that the code executes successfully but returns the wrong result. Shouldn't &H8000' date=' &H3FFFFFFF, &H1021 and &HFFFF be defined somewhere?[/Quote'] They probably should have been defined as constants, but they weren't and this isn't a question about code practices. 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. Edited November 3, 2005 by snarfblam Quote [sIGPIC]e[/sIGPIC]
SonicBoomAu Posted November 3, 2005 Posted November 3, 2005 Well you learn something new everyday. Thanks for that Marble Eater. Quote Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -- Rick Cook, The Wizardry Compiled
otherside Posted November 3, 2005 Author Posted November 3, 2005 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. Quote
otherside Posted November 4, 2005 Author Posted November 4, 2005 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 .... Quote
Leaders snarfblam Posted November 4, 2005 Leaders Posted November 4, 2005 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. Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.