PC Review


Reply
Thread Tools Rate Thread

Checksum hepl

 
 
Lou
Guest
Posts: n/a
 
      29th Apr 2009
I have a VB6 function that I cannot for the life of me translate int C#.
I keep getting conversion errors.
Any help would be appreciated.

-Lou



Public Function CalculateCheckSumBytes(bData() As Byte) As Byte
Dim Cks As Long
Dim bCks As Byte
Dim iByteCount As Long
Dim i As Long

iByteCount = UBound(bData)

Cks = 0

For i = 0 To iByteCount
Cks = Cks + bData(i)
Next i

Cks = (-Cks)
bCks = Cks And &HFF&

CalculateCheckSumBytes = bCks
End Function


 
Reply With Quote
 
 
 
 
Pavel Minaev
Guest
Posts: n/a
 
      29th Apr 2009
On Apr 29, 10:53*am, "Lou" <lou.gar...@comcast.net> wrote:
> I have a VB6 function that I cannot for the life of me translate int C#.
> I keep getting conversion errors.
> Any help would be appreciated.
>
> -Lou
>
> Public Function CalculateCheckSumBytes(bData() As Byte) As Byte
> * * Dim Cks * * * * As Long
> * * Dim bCks * * * *As Byte
> * * Dim iByteCount *As Long
> * * Dim i * * * * * As Long
>
> * * iByteCount = UBound(bData)
>
> * * Cks = 0
>
> * * For i = 0 To iByteCount
> * * * * Cks = Cks + bData(i)
> * * Next i
>
> * * Cks = (-Cks)
> * * bCks = Cks And &HFF&
>
> * * CalculateCheckSumBytes = bCks
> End Function


byte CalculateCheckSumBytes(byte[] data) {
return (byte)(-data.Select(b => (int)b).Sum() & 0xFF);
}
 
Reply With Quote
 
Jeroen Mostert
Guest
Posts: n/a
 
      29th Apr 2009
Lou wrote:
> I have a VB6 function that I cannot for the life of me translate int C#.
> I keep getting conversion errors.
> Any help would be appreciated.
>
> Public Function CalculateCheckSumBytes(bData() As Byte) As Byte
> Dim Cks As Long
> Dim bCks As Byte
> Dim iByteCount As Long
> Dim i As Long
>
> iByteCount = UBound(bData)
>
> Cks = 0
>
> For i = 0 To iByteCount
> Cks = Cks + bData(i)
> Next i
>
> Cks = (-Cks)
> bCks = Cks And &HFF&
>
> CalculateCheckSumBytes = bCks
> End Function
>

Well, a *true* checksum function, don't see much of those. Anyhow, this
should do the same trick:

public static byte CalculateChecksum(byte[] data) {
int checksum;
for (int i = 0; i != data.Length; ++i) {
unchecked { checksum += data[i]; }
}
return (byte) (-checksum & 0xff);
}

--
J.
 
Reply With Quote
 
Jeroen Mostert
Guest
Posts: n/a
 
      29th Apr 2009
Jeroen Mostert wrote:
> public static byte CalculateChecksum(byte[] data) {
> int checksum;


Well, of course, the shorter a function is the more likely you are to put in
errors anyway... "checksum" needs to be initialized to 0, of course.

--
J.
 
Reply With Quote
 
christery@gmail.com
Guest
Posts: n/a
 
      29th Apr 2009
byte[] bData = { 1, 2, 3, 4, 5 };
long Cks;
byte bCks;
long iByteCount;
long i;

//Really want Ubound, it can still be found...
iByteCount = Microsoft.VisualBasic.Information.UBound
(bData, 1);
Cks = 0;
for (i = 0; i <= iByteCount; i++)
Cks = Cks + bData[i];
Cks = -Cks;
bCks = (Byte)(Cks & 0xff);
textBox1.Text=bCks.ToString(); // Just to put it somwhere
for testing

But J was more C# ish...

//CY
 
Reply With Quote
 
Lou
Guest
Posts: n/a
 
      30th Apr 2009
Awesome, works like a charm!


"Jeroen Mostert" <(E-Mail Removed)> wrote in message
news:49f8a98b$0$193$(E-Mail Removed)...
> Lou wrote:
>> I have a VB6 function that I cannot for the life of me translate int C#.
>> I keep getting conversion errors.
>> Any help would be appreciated.
>>
>> Public Function CalculateCheckSumBytes(bData() As Byte) As Byte
>> Dim Cks As Long
>> Dim bCks As Byte
>> Dim iByteCount As Long
>> Dim i As Long
>>
>> iByteCount = UBound(bData)
>>
>> Cks = 0
>>
>> For i = 0 To iByteCount
>> Cks = Cks + bData(i)
>> Next i
>>
>> Cks = (-Cks)
>> bCks = Cks And &HFF&
>>
>> CalculateCheckSumBytes = bCks
>> End Function

> Well, a *true* checksum function, don't see much of those. Anyhow, this
> should do the same trick:
>
> public static byte CalculateChecksum(byte[] data) {
> int checksum;
> for (int i = 0; i != data.Length; ++i) {
> unchecked { checksum += data[i]; }
> }
> return (byte) (-checksum & 0xff);
> }
>
> --
> J.



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
i need hepl SANDRE PARKER Microsoft Access 3 15th Apr 2006 11:45 PM
hepl Jonny Windows XP General 5 6th Apr 2006 03:04 AM
How to handle TCP checksum, if adapter support TCP checksum offloading? Rajesh Gupta Windows XP Drivers 0 3rd Aug 2004 12:20 AM
Stop Bad Image Checksum Image lz32.dll is corrupt. The header checksum does not TJ Microsoft Windows 2000 File System 0 1st Apr 2004 04:10 PM
GPO HEPL!!! carlos Microsoft Windows 2000 Active Directory 3 19th Dec 2003 06:28 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:58 AM.