Need to convert the following function from c# to vb.net

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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;
}
}
}
 
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;
}
}
}
 
Thanks,

Converted Code Below, But now have the problem of int can not be converted to Uint32 (At Line >>)
Public Shared Function checksum(ByVal buffer() As UInt16, ByVal size As Integer) As UInt16
Dim cksum As Int32 = 0
Dim counter As Integer
counter = 0

While size > 0
Dim val As UInt16 = buffer(counter)

cksum += Convert.ToInt32(buffer(counter))
counter += 1
size -= 1
End While

cksum = (cksum > 16) + (cksum And &HFFFF)
cksum += (cksum > 16)
End Function
 
Change Dim chksum as Int32 to Int16 and remove the Convert.To.int32 stuff
 
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);
}
}
 
Yes, your right, and interestly enough, using the concatenation operator on
integers does exactly that it concatenates them. The following code displays
'10'.

Dim a As Int16
Dim b As Int16
Dim c As Int16

a = 1
b = 0
c = a & b

MsgBox(c.ToString)
--

OHM ( Terry Burns )
. . . One-Handed-Man . . .



Claes Bergefall said:
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);
}
}
 
* =?Utf-8?B?R2xlbm4gV2lsc29u?= 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];
^^^
What's the purpose of this variable?
 
Back
Top