473,405 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 1745
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
8
by: John Bowman | last post by:
Hello, Does anyone have a good/reliable approach to implementing an IsNumeric() method that accepts a string that may represent a numerical value (eg. such as some text retrieved from an XML...
5
by: Ed Chiu | last post by:
Hi, I have a Dataset named dsCMDetail, there is only one data table in the dataset, the following code snippet does not compile: Dim dtCMDetail as datatable dtCMDetail = dsCMDetail.table(0)...
6
by: patang | last post by:
Could someone please tell me where am I supposed to put this code. Actually my project has two forms. I created a new module and have put the following code sent by someone. All the function...
5
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public...
21
by: asif929 | last post by:
I need immediate help in writing a function program. I have to write a program in functions and use array to store them. I am not familiar with functions and i tried to create it but i fails to...
4
by: =?Utf-8?B?Ym9va2VyQG1ndA==?= | last post by:
Ok, I inherited some code written in vb that is part of a web application. My overall objective is to be able to take multiple names from a "LastName" text box and use those names in my SQL query...
1
by: Dancefire | last post by:
Hi, everyone, I'm trying to use std::codecvt<to do the encoding conversion. I am using following code for encoding conversion between wchar_t string and char string(MBCS). I am not sure am I...
15
by: active | last post by:
Below is a small but complete program that appears to show you can't retrive a Palette from the clipboard. This is true whether the palette is placed on the clipboard by Photoshop or Photoshop...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.