Checksum hepl

  • Thread starter Thread starter Lou
  • Start date Start date
L

Lou

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
 
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);
}
 
Lou said:
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; }
}
return (byte) (-checksum & 0xff);
}
 
Jeroen said:
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.
 
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;
Cks = -Cks;
bCks = (Byte)(Cks & 0xff);
textBox1.Text=bCks.ToString(); // Just to put it somwhere
for testing

But J was more C# ish...

//CY
 
Awesome, works like a charm!


Jeroen Mostert said:
Lou said:
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; }
}
return (byte) (-checksum & 0xff);
}
 
Back
Top