That's one of the worst conversions I've ever seen:
1. & is incorrect in VB.NET. It's called And
2. 0xffff won't compile. It should be &HFFFF
3. > in VB.NET is not the same as >> in C#
The later is a shift operator and the former
is a relational operator. >> is supported in
VB.NET aswell
4. ~ is incorrect in VB.NET. Use Not
Try this:
Public Shared Function Checksum(ByVal buffer() As UInt16, ByVal size As
Integer) As UInt16
Dim chksum As Int32
Dim counter As Integer
For counter = 0 To size - 1
chksum += System.Convert.ToInt32(buffer(counter))
Next
chksum = (chksum >> 16) + (chksum And &HFFFF)
chksum += (chksum >> 16)
' Need "And &HFFFF" to prevent overflow exception
Return System.Convert.ToUInt16((Not chksum) And &HFFFF)
End Function
/claes
Todd Zetlan said:
You may want to try the following URL which provides a conversion utility:
http://www.kamalpatel.net/ConvertCSharp2VB.aspx. The result they provide
is:
cksum = (cksum > 16) + (cksum & 0xffff)
cksum += (cksum > 16)
Return CType((~cksum), UInt16)
--
TODD ZETLAN
Glenn Wilson said:
I have converted most of the code that I have but am having trouble,
mainly with the marked lines. (>>)
public static UInt16 checksum( UInt16[] buffer, int size )
{
Int32 cksum = 0;
int counter;
counter = 0;
while ( size > 0 ) {
UInt16 val = buffer[counter];
cksum += Convert.ToInt32( buffer[counter] );
counter += 1;
size -= 1;
}
cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >> 16);
return (UInt16)(~cksum);
}
}