469,626 Members | 1,369 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,626 developers. It's quick & easy.

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

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);

}
}
Nov 20 '05 #1
6 1571
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" <Gl*********@discussions.microsoft.com> wrote in message
news:0B**********************************@microsof t.com...
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);

}
}

Nov 20 '05 #2
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)
Return CType(cksum, UInt16)

End Function

"Todd Zetlan" wrote:
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


Nov 20 '05 #3
Change Dim chksum as Int32 to Int16 and remove the Convert.To.int32 stuff

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
"Glenn Wilson" <Gl*********@discussions.microsoft.com> wrote in message
news:30**********************************@microsof t.com...
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)
Return CType(cksum, UInt16)
End Function

"Todd Zetlan" wrote:
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

Nov 20 '05 #4
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" <ze****@verizon.net> wrote in message
news:Cj*****************@nwrdny02.gnilink.net...
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" <Gl*********@discussions.microsoft.com> wrote in message
news:0B**********************************@microsof t.com...
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);

}
}


Nov 20 '05 #5
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" <cl*************@online.nospam> wrote in message
news:eb**************@tk2msftngp13.phx.gbl...
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" <ze****@verizon.net> wrote in message
news:Cj*****************@nwrdny02.gnilink.net...
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" <Gl*********@discussions.microsoft.com> wrote in message
news:0B**********************************@microsof t.com...
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);
}
}



Nov 20 '05 #6
* =?Utf-8?B?R2xlbm4gV2lsc29u?= <Gl*********@discussions.microsoft.com> scripsit:
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?

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Timothy Shih | last post: by
8 posts views Thread by John Bowman | last post: by
5 posts views Thread by Ed Chiu | last post: by
6 posts views Thread by patang | last post: by
21 posts views Thread by asif929 | last post: by
4 posts views Thread by =?Utf-8?B?Ym9va2VyQG1ndA==?= | last post: by
1 post views Thread by Dancefire | last post: by
15 posts views Thread by active | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.